2017-04-01 133 views

回答

3

由於在XWPFNumbering新增加一個新的AbstractNum,需要XWPFAbstractNum,這是無法創建,而無需使用計算機墊低級別對象到現在爲止,至少org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum是必要的。

CTAbstractNum有創建其中的單個子對象的方法。但最終的代碼將是非常小的部分代碼,並且需要太多的代碼行。所以在我看來,最好的辦法是解析XML來創建AbstractNum。爲了得到這個XML可以創建一個簡單的Word文件,然後解壓*.docx文件並看看/word/numbering.xml。在那裏你會找到類似的東西:

<w:numbering> 
<w:abstractNum w:abstractNumId="0"> 
    <w:multiLevelType w:val="hybridMultilevel"/> 
    <w:lvl w:ilvl="0"> 
    <w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1"/><w:lvlJc w:val="left"/> 
    <w:pPr> 
    <w:ind w:hanging="360" w:left="720"/> 
    </w:pPr> 
    </w:lvl> 
    <w:lvl w:ilvl="1" w:tentative="1"> 
    <w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1.%2"/><w:lvlJc w:val="left"/> 
    <w:pPr> 
    <w:ind w:hanging="360" w:left="1440"/> 
    </w:pPr> 
    </w:lvl> 
    <w:lvl w:ilvl="2" w:tentative="1"> 
    <w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1.%2.%3"/><w:lvlJc w:val="left"/> 
    <w:pPr> 
    <w:ind w:hanging="360" w:left="2160"/> 
    </w:pPr> 
    </w:lvl> 
</w:abstractNum> 
... 
</w:numbering> 

例如3級十進制編號。

這個XML在我看來是自我解釋。每個編號級別都有一個lvl定義,其定義爲它的外觀和縮進。

對於子彈編號的XML將是類似的,但對於使用的將是附加的符號字符的字形定義使用Font定義。例如,「黑色立方體」是來自字體Wingdings的字形。

... 
<w:lvl w:ilvl="0"> 
<w:start w:val="1"/><w:numFmt w:val="bullet"/><w:lvlText w:val=""/><w:lvlJc w:val="left"/> 
<w:pPr> 
    <w:ind w:hanging="360" w:left="720"/> 
</w:pPr> 
<w:rPr> 
    <w:rFonts w:ascii="Wingdings" w:hAnsi="Wingdings" w:hint="default"/> 
</w:rPr> 
</w:lvl> 
... 

知道了這一點,我們可以將代碼:

import java.io.File; 
import java.io.FileOutputStream; 

import org.apache.poi.xwpf.usermodel.*; 

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering; 

import java.math.BigInteger; 

public class CreateWordBulletOrDecimalList { 

static String cTAbstractNumBulletXML = 
    "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"0\">" 
+ "<w:multiLevelType w:val=\"hybridMultilevel\"/>" 
+ "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Wingdings\" w:hAnsi=\"Wingdings\" w:hint=\"default\"/></w:rPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"-\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Courier New\" w:hAnsi=\"Courier New\" w:cs=\"Courier New\" w:hint=\"default\"/></w:rPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Symbol\" w:hAnsi=\"Symbol\" w:hint=\"default\"/></w:rPr></w:lvl>" 
+ "</w:abstractNum>"; 

static String cTAbstractNumDecimalXML = 
    "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"0\">" 
+ "<w:multiLevelType w:val=\"hybridMultilevel\"/>" 
+ "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2.%3\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr></w:lvl>" 
+ "</w:abstractNum>"; 

public static void main(String[] args) throws Exception { 

    XWPFDocument document = new XWPFDocument(); 

    XWPFParagraph paragraph = document.createParagraph(); 
    XWPFRun run=paragraph.createRun(); 
    run.setText("The List:"); 

    CTNumbering cTNumbering = CTNumbering.Factory.parse(cTAbstractNumBulletXML); 
    //CTNumbering cTNumbering = CTNumbering.Factory.parse(cTAbstractNumDecimalXML); 

    CTAbstractNum cTAbstractNum = cTNumbering.getAbstractNumArray(0); 

    XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum); 

    XWPFNumbering numbering = document.createNumbering(); 

    BigInteger abstractNumID = numbering.addAbstractNum(abstractNum); 

    BigInteger numID = numbering.addNum(abstractNumID); 

    for (int i = 0; i < 5; i++) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID); 
    run = paragraph.createRun(); 
    run.setText("List item " + (i+1)); 
    if (i < 4) paragraph.setSpacingAfter(0); 
    if (i == 0) { 
    for (int j = 0; j < 2; j++) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID); 
    paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1)); 
    run = paragraph.createRun(); 
    run.setText("Sub list item " + (i+1) + " " + (char)(97+j)); 
    paragraph.setSpacingAfter(0); 
    } 
    } 
    if (i == 1 || i == 3) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID); 
    paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1)); 
    run = paragraph.createRun(); 
    run.setText("Sub list item " + (i+1) + " a"); 
    paragraph.setSpacingAfter(0); 
    } 
    } 

    paragraph = document.createParagraph(); 
    run=paragraph.createRun(); 
    run.setText("Paragraph after the list."); 

    FileOutputStream out = new FileOutputStream("CreateWordBulletOrDecimalList.docx");  
    document.write(out); 

    System.out.println("CreateWordBulletOrDecimalList written successully"); 
} 
} 

代碼在一個文檔中使用這兩種類型的列表:

import java.io.File; 
import java.io.FileOutputStream; 

import org.apache.poi.xwpf.usermodel.*; 

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering; 

import java.math.BigInteger; 

public class CreateWordBulletAndDecimalList { 

static String cTAbstractNumBulletXML = 
    "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"0\">" 
+ "<w:multiLevelType w:val=\"hybridMultilevel\"/>" 
+ "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Wingdings\" w:hAnsi=\"Wingdings\" w:hint=\"default\"/></w:rPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"-\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Courier New\" w:hAnsi=\"Courier New\" w:cs=\"Courier New\" w:hint=\"default\"/></w:rPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Symbol\" w:hAnsi=\"Symbol\" w:hint=\"default\"/></w:rPr></w:lvl>" 
+ "</w:abstractNum>"; 

static String cTAbstractNumDecimalXML = 
    "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"1\">" 
+ "<w:multiLevelType w:val=\"hybridMultilevel\"/>" 
+ "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr></w:lvl>" 
+ "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2.%3\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr></w:lvl>" 
+ "</w:abstractNum>"; 

public static void main(String[] args) throws Exception { 

    XWPFDocument document = new XWPFDocument(); 

    CTNumbering cTNumbering = CTNumbering.Factory.parse(cTAbstractNumBulletXML); 
    CTAbstractNum cTAbstractNum = cTNumbering.getAbstractNumArray(0); 
    XWPFAbstractNum abstractNum1 = new XWPFAbstractNum(cTAbstractNum); 


    cTNumbering = CTNumbering.Factory.parse(cTAbstractNumDecimalXML); 
    cTAbstractNum = cTNumbering.getAbstractNumArray(0); 
    XWPFAbstractNum abstractNum2 = new XWPFAbstractNum(cTAbstractNum); 

    XWPFNumbering numbering = document.createNumbering(); 

    BigInteger abstractNumID1 = numbering.addAbstractNum(abstractNum1); 
    BigInteger numID1 = numbering.addNum(abstractNumID1); 

    BigInteger abstractNumID2 = numbering.addAbstractNum(abstractNum2); 
    BigInteger numID2 = numbering.addNum(abstractNumID2); 

    XWPFParagraph paragraph = document.createParagraph(); 
    XWPFRun run=paragraph.createRun(); 
    run.setText("The lists:"); 
    paragraph = document.createParagraph(); 

    paragraph = document.createParagraph(); 
    run=paragraph.createRun(); 
    run.setText("The bullet list:"); 

    for (int i = 0; i < 5; i++) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID1); 
    run = paragraph.createRun(); 
    run.setText("List item " + (i+1)); 
    if (i < 4) paragraph.setSpacingAfter(0); 
    if (i == 0) { 
    for (int j = 0; j < 2; j++) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID1); 
    paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1)); 
    run = paragraph.createRun(); 
    run.setText("Sub list item " + (i+1) + " " + (char)(97+j)); 
    paragraph.setSpacingAfter(0); 
    } 
    } 
    if (i == 1 || i == 3) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID1); 
    paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1)); 
    run = paragraph.createRun(); 
    run.setText("Sub list item " + (i+1) + " a"); 
    paragraph.setSpacingAfter(0); 
    } 
    } 

    paragraph = document.createParagraph(); 
    paragraph = document.createParagraph(); 
    run=paragraph.createRun(); 
    run.setText("The decimal list:"); 

    for (int i = 0; i < 5; i++) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID2); 
    run = paragraph.createRun(); 
    run.setText("List item " + (i+1)); 
    if (i < 4) paragraph.setSpacingAfter(0); 
    if (i == 0) { 
    for (int j = 0; j < 2; j++) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID2); 
    paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1)); 
    run = paragraph.createRun(); 
    run.setText("Sub list item " + (i+1) + " " + (char)(97+j)); 
    paragraph.setSpacingAfter(0); 
    } 
    } 
    if (i == 1 || i == 3) { 
    paragraph = document.createParagraph(); 
    paragraph.setNumID(numID2); 
    paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1)); 
    run = paragraph.createRun(); 
    run.setText("Sub list item " + (i+1) + " a"); 
    paragraph.setSpacingAfter(0); 
    } 
    } 

    paragraph = document.createParagraph(); 
    paragraph = document.createParagraph(); 
    run=paragraph.createRun(); 
    run.setText("Paragraph after the lists."); 

    FileOutputStream out = new FileOutputStream("CreateWordBulletAndDecimalList.docx");  
    document.write(out); 

    System.out.println("CreateWordBulletAndDecimalList written successully"); 
} 
} 
+0

如果我用cTAbstractNumBulletXML和cTAbstractNumDecimalXML兩個,我發現後者將代替以前。有兩種方法getList1(),getList2(),分別使用cTAbstractNumBulletXML和cTAbstractNumDecimalXML,但getList1()會變成getList2()的風格,它有什麼問題? – Sucy

+0

我的示例代碼是或者**,使用'cTAbstractNumBulletXML' ** **或**使用'cTAbstractNumDecimalXML'。如果你同時需要,你需要兩個不同的'XWPFAbstractNum'' abstractNum1'和'abstractNum2'這兩個不同的'XWPFNumbering編號',從而產生兩個不同的'BigInteger'' abstractNumID1'和'abstractNumID2'以及兩個不同的'BigInteger'' numID1'和'numID2'。 'paragraph.setNumID'中使用的不同numID將爲段落設置不同種類的編號。 –

+0

忘了一件事:'w:abstractNumId =「0」'不能在'XML'中同時爲'0'。其中一個必須是不同的。現在我已經提供了使用這兩種列表的示例代碼。 –