答案 0 :(得分:5)
由于新添加了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
将类似,但使用的{bullet}将使用Font
定义进行额外定义。 &#34;黑色立方体&#34;例如,是字体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");
}
}