如何以编程方式在Word文件中创建索引?

时间:2018-06-27 05:43:48

标签: plugins ms-word swt ole docx4j

假设有上千个数据项的表,我们需要创建任何项的索引。 因此,如何在docx4j的帮助下以编程方式在Docx文件中创建索引。

1 个答案:

答案 0 :(得分:0)

 MainDocumentPart mdp = word.getMainDocumentPart();
        String textXpath = "//w:t";
        List<Object> textNodes= mdp.getJAXBNodesViaXPath(textXpath, true);
        c=0;
        for (Object obj : textNodes)
        {
            Text text = (Text) ((JAXBElement<?>) obj).getValue();       
            textValue = text.getValue();
         String[] words = textValue.split("\\W+");
            for (String word : words) 
            {
             word = word.toLowerCase();
             List<Integer> list = occurences.get(word);

             if (list == null) 
             {
                 list = new ArrayList<Integer>();
                 occurences.put(word, list);
             }

             list.add(c);
         }

         c++;

        }
        //System.out.print(occurences.toString());
        word.getMainDocumentPart().addParagraphOfText("");
        word.getMainDocumentPart().addParagraphOfText("");
        word.getMainDocumentPart().addStyledParagraphOfText("Title", "INDEX");
        String count = occurences.toString();
        word.getMainDocumentPart().addParagraphOfText("count");