【问题标题】:Insert a bulleted list from an ArrayList Apache POI XWPF从 ArrayList Apache POI XWPF 插入项目符号列表
【发布时间】:2020-07-30 18:47:25
【问题描述】:

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

我已经有编号(带数字),我想在不同的列表中同时拥有(数字和项目符号)。

我的文档已预先填充了一些数据,并且我有一些标记来确定我的数据的去向。对于我的列表,我有类似这个的令牌,并且我能够到达它。

  • {{tokenlist1}}

我想:

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

第二个选项:用我的第一个元素替换我的标记并继续我的项目符号列表。

如果项目符号形式(方形、圆形、检查、....)可以与令牌保持相同,将不胜感激。

【问题讨论】:

    标签: java apache-poi xwpf


    【解决方案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();
                TextSegment 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(TextSegment 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);
            }
        }
    }
    

    【讨论】:

    • 那么项目符号呢?
    • 我已经有一段时间没有编写该代码了,但是项目符号点已添加到模板中,并且该代码会自动添加以下项目符号点。我可以确认一下,但我很确定确实如此。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多