问题
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