段落.getRuns()分隔段落 [英] paragraph.getRuns() to separate a paragraph

查看:586
本文介绍了段落.getRuns()分隔段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Apache poi自动更改合同的日期时,我对如何段段落感到很困惑.getRuns()分隔段落.我有以下一段 自2014年10月1日起至2014年10月31日止

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 日止

我使用以下代码来查看段落.getRuns()返回多少XWPFRun

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);
                }

我发现前五个数字都是xwpfRun独立的,例如,2014,10,1 但最后一个数字"31"被分为两个xwpfRun:"3"和"1";

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";

这很难通过xwpfRun更改日期,我想知道如何处理日期以及段落.getRuns()如何工作?

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?

推荐答案

有时DOCX文件中的文本被分解为任意数量的运行.虽然不方便,但处理起来并不困难.

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 "".

这篇关于段落.getRuns()分隔段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆