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

查看:58
本文介绍了段落.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 日止

我用下面的代码来看看paragraph.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 更改日期变得困难,我想知道如何处理这个以及paragraph.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天全站免登陆