Word文档中的Apache POI圆形项目符号列表

时间:2016-03-09 16:04:09

标签: java apache ms-word xwpf

我想使用Java创建word文档,并希望在文档中添加项目符号列表。子弹必须是圆形或复选标记而不是数字。我能够使用XWPF创建一个数字子弹列表,但不能创建圆形或复选标记项目符号。请分享一些示例,说明如何使用Java在单词中创建圆形/复选标记类型项目符号。

1 个答案:

答案 0 :(得分:1)

这很难解释,但这是一个功能:

public static BigInteger addListStyle(XWPFDocument doc, char symbol) throws XmlException{

        String styleMaybe = "<w:numbering xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" mc:Ignorable=\"w14 w15 wp14\">\n" + 
                                        "<w:abstractNum w:abstractNumId=\""+listStyleIDCounter+"\">\n" + 
                                                "<w:nsid w:val=\"6871722E\"/>\n" + 
                                                "<w:multiLevelType w:val=\"hybridMultilevel\"/>\n" + 
                                                "<w:tmpl w:val=\"8FE6E4C8\"/>\n" + 
                                                "<w:lvl w:ilvl=\"0\" w:tplc=\"0410000D\">\n" + 
                                                        "<w:start w:val=\"1\"/>\n" + 
                                                        "<w:numFmt w:val=\"bullet\"/>\n" + 
                                                        "<w:lvlText w:val=\""+symbol+"\"/>\n" + 
                                                        "<w:lvlJc w:val=\"left\"/>\n" + 
                                                        "<w:pPr>\n" + 
                                                                "<w:ind w:left=\"720\" w:hanging=\"360\"/>\n" + 
                                                        "</w:pPr>\n" + 
                                                                "<w:rPr>\n" + 
                                                                "<w:rFonts w:ascii=\"Webdings\" w:hAnsi=\"Webdings\" w:hint=\"default\"/>\n" + 
                                                        "</w:rPr>\n" + 
                                                "</w:lvl>\n" + 
                                        "</w:abstractNum>\n" + 
                                        "<w:num w:numId=\"1\">\n" + 
                                        "<w:abstractNumId w:val=\"0\"/>\n" + 
                                        "</w:num>\n" + 
                                "</w:numbering>";



    XWPFNumbering numbering = doc.createNumbering();

    // genero il numbering style dall'XML
    CTAbstractNum abstractNum = CTAbstractNum.Factory.parse(styleMaybe);
    XWPFAbstractNum abs = new XWPFAbstractNum(abstractNum, numbering);
    // gli imposto un ID univoco
    BigInteger id = BigInteger.valueOf(listStyleIDCounter++);

    // assegno l'id all'abs
    abs.getAbstractNum().setAbstractNumId(id);

    // ora aggiungo l'abs al CT del numbering, che mi dovrebbe ritornare lo stesso id
    id = numbering.addAbstractNum(abs);
    // ora lo aggiungo al numbering creato prima che mi restituirà ancora lo stesso id
    return doc.getNumbering().addNum(id);
}

其中listStyleIDCounter是从0开始的静态计数器。

您可以将a传递给检查符号,将=传递给小圆圈,将<传递给小方块,以及其他可以自行尝试的符号:D