从ArrayList Apache POI XWPF插入项目符号列表

时间:2015-12-15 20:41:09

标签: java apache-poi xwpf

我有一个数组列表,我想用它来在文档中创建一个新的项目符号列表。

我已经有编号(有数字),我想在不同的列表上同时拥有(数字和子弹)。

我的文档预先填充了一些数据,我有一些令牌确定我的数据在哪里。对于我的列表,我有一个像这样的令牌,我能够达到它。

  • {{tokenlist1}}

我想:

第一个选项:到达我的令牌,创建新的项目符号列表并删除我的令牌

第二个选项:用我的第一个元素替换我的令牌并继续我的子弹列表。

如果子弹形式(方形,圆形,支票,......)可以与令牌保持一致,我们将非常感激。

1 个答案:

答案 0 :(得分:1)

修改

对于那些想要回答的人来说,这是我的解决方案。

<强>动作

Map<String, Object> replacements = new HashMap<String, Object>();
        replacements.put("{{token1}}", "texte changé 1");
        replacements.put("{{token2}}", "ici est le texte du token numéro 2");
        replacements.put("{{tokenList1}}", tokenList1);
        replacements.put("{{tokenList2}}", tokenList1);         

        templateWithToken = reportService.findAndReplaceToken(replacements, templateWithToken);

<强>服务

Public XWPFDocument findAndReplaceToken (Map<String, Object> replacements,
        XWPFDocument document) {
    List<XWPFParagraph> paragraphs = document.getParagraphs();
    for (int i = 0; i < paragraphs.size(); i++) {
        XWPFParagraph paragraph = paragraphs.get(i);

        List<XWPFRun> runs = paragraph.getRuns();
        for (Map.Entry<String, Object> replPair : replacements
                .entrySet()) {
            String find = replPair.getKey();
            Object repl = replPair.getValue();
            TextSegement found =
                    paragraph.searchText(find, new PositionInParagraph());
            if (found != null) {
                if (repl instanceof String) {
                    replaceText(found, runs, find, repl);
                } else if (repl instanceof ArrayList<?>) {

                    Iterator<?> iterArrayList =
                            ((ArrayList) repl).iterator();

                    boolean isPassed = false;

                    while (iterArrayList.hasNext()) {
                        Object object = (Object) iterArrayList.next();

                        if (isPassed == false) {
                            replaceText(found, runs, find,
                                    object.toString());
                        } else {
                            XWPFRun run = paragraph.createRun();
                            run.addCarriageReturn();
                            run.setText(object.toString());
                        }

                        isPassed = true;
                    }
                }
            }
        }
    }
    return document;
}

private void replaceText(TextSegement found, List<XWPFRun> runs,
        String find, Object repl) {
    int biginRun = found.getBeginRun();
    int biginRun2 = found.getEndRun();
    if (found.getBeginRun() == found.getEndRun()) {
        // whole search string is in one Run
        XWPFRun run = runs.get(found.getBeginRun());
        String runText = run.getText(run.getTextPosition());
        String replaced = runText.replace(find, repl.toString());
        run.setText(replaced, 0);
    } else {
        // The search string spans over more than one Run
        // Put the Strings together
        StringBuilder b = new StringBuilder();
        for (int runPos = found.getBeginRun(); runPos <= found
                .getEndRun(); runPos++) {
            XWPFRun run = runs.get(runPos);
            b.append(run.getText(run.getTextPosition()));
        }
        String connectedRuns = b.toString();
        String replaced = connectedRuns.replace(find, repl.toString());

        // The first Run receives the replaced String of all
        // connected Runs
        XWPFRun partOne = runs.get(found.getBeginRun());
        partOne.setText(replaced, 0);
        // Removing the text in the other Runs.
        for (int runPos = found.getBeginRun() + 1; runPos <= found
                .getEndRun(); runPos++) {
            XWPFRun partNext = runs.get(runPos);
            partNext.setText("", 0);
        }
    }
}