paragraph.getRuns() to separate a paragraph

☆樱花仙子☆ 提交于 2020-01-03 05:24:09

问题


When I use Apache poi to change the date of a contract automatically, i am very confused at the how dose paragraph.getRuns() separate a paragraph. I have the following paragraph 自 2014 年 10 月 1 日起至 2014 年 10 月 31 日止

I use the following code to see how many XWPFRun does paragraph.getRuns() return

String currentParagraph = "";
                for(XWPFRun xwpfRun : paragraph.getRuns()){
                    currentParagraph += xwpfRun.getText(0);
                    System.out.println(currentParagraph);
                }

I find the first five number are all a xwpfRun independently,eg ,2014,10,1 but the last number "31" was separated into two xwpfRun :"3", and "1";

This makes it hard to change the date by xwpfRun ,and I want to know How to deal with this and how does paragraph.getRuns() works?


回答1:


Sometimes text in DOCX files gets broken up into an arbitrary number of runs. While inconvenient, it's not to difficult to deal with.

The solution is to iterate all runs in the paragraph, and concatenate the text from each to a string. Then, update the date and store it as the text of the first run. Lastly, you can remove or set the text in the other runs to "".



来源:https://stackoverflow.com/questions/27243737/paragraph-getruns-to-separate-a-paragraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!