Apache Poi 操作word,替换字符保留样式问题,runs段落混乱问题。
关于这个问题也是刚好遇到,一通搜索也没有找到类似的或者是有效的方法。下面介绍一下。
首先apache poi的引入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency> <!-- poi_tl 工具,仅支持docx且友好
开源,官方文档:http://deepoove.com/poi-tl/1.10.x/
-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.0</version>
</dependency>
上面的包都是一些基础的东西,然后需要注意的是版本问题。因为版本不一样可能导致的用法也不一样。 poi-tl 1.10.0 版本需要poi 4.1.2的版本来支持。这个官方的作者已经说了。
下面直接上 替换word的代码
XWPFDocument document = new XWPFDocument(in);
List<XWPFParagraph> paragraphs = document.getParagraphs();
Map<String, String> replacements = new HashMap<>();
//这里是找回替换的特殊字符,我是通过正则去找回的。因为我的比较多。而且我一般习惯${}的写法。
//当然poi-tl也可以直接替换很方便,但是这里用的是原生的apache poi。因为担心poi-tl还不是很成熟。
List<String> replaceFields = retrieveReplaceFields(document, regex);
for (String replaceField : replaceFields) {
String factField = StringUtils.substringBetween(replaceField, prefix, suffix);
String val = retrieveData(factField, data);
replacements.put(replaceField,val);
}
//这里是普通段落
replaceInParagraphs(replacements,paragraphs);
//处理表格
Iterator<XWPFTable> iterator = document.getTablesIterator();
while (iterator.hasNext()) {
XWPFTable table = iterator.next();
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
List<XWPFTableCell> tableCells = row.getTableCells();
for (XWPFTableCell cell : tableCells) {
List<XWPFParagraph> cellParagraphs = cell.getParagraphs();
replaceInParagraphs(replacements,cellParagraphs);
}
}
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
document.write(output);
document.write(new FileOutputStream("C:\\Users\\dato\\Desktop\\dato.docx"));
这其中最重要替换方法来了:
replaceInParagraphs(replacements,cellParagraphs);
private static long replaceInParagraphs(Map<String, String> replacements, List<XWPFParagraph> xwpfParagraphs) {
long count = 0;
for (XWPFParagraph paragraph : xwpfParagraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (Map.Entry<String, String> replPair : replacements.entrySet()) {
String find = replPair.getKey();
String repl = replPair.getValue();
TextSegment found = paragraph.searchText(find, new PositionInParagraph());
if ( found != null ) {
count++;
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);
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);
// 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);
}
}
}
}
}
return count;
}
Apache Poi 操作word,替换字符保留样式问题,runs段落混乱问题。的更多相关文章
- 用C#操作word替换字符,用spire
这两天想写个小程序,是用C#操作word文档的.许多人都对微软本身的解决方案COM组件十分不看好,比如需要本机安装office等等,总之吐槽很多,直接放弃. 搜到一个国产的npoi库,据说操作简单功能 ...
- 利用Apache POI操作Excel
最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中.简单回顾一下之前学过的用java操作Excel. 1.maven配置Apache POI pom.xml中配置POIjar ...
- java使用poi操作word, 支持动态的行(一个占位符插入多条)和表格中动态行, 支持图片
依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifa ...
- poi操作word 2007 常用方法总结
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- 使用java Apache poi 根据word模板生成word报表
项目开发过程中,客户提出一堆导出报表的需求,需要导出word格式,页眉还需要加上客户公司的logo,试了几种方案,最后选择了用 Apache poi 加上自定义标签的方式实现. 目前功能还比较简单,一 ...
- Java操作word文档使用JACOB和POI操作word,Excel,PPT需要的jar包
可参考文档: http://wibiline.iteye.com/blog/1725492 下载jar包 http://download.csdn.net/download/javashixiaofe ...
- apache POI 操作excel<导入导出>
1.首先导入maven依赖 <!-- POI核心依赖 --> <dependency> <groupId>org.apache.poi</groupId> ...
- 利用poi操作word文档
关键字:POI JAVA 批注 总页数 总字符数 一:认识POI Apache POI是一个开源的利用Java读写Excel.WORD等微软OLE2组件文档的项目.最新的3.5版本有很多改进,加入了对 ...
- 关于 HSSF 和 XSSF 功能的开发者入门指南 (Apache POI 操作 Excel)
关于 HSSF 和 XSSF 功能的开发者入门指南 笔者深夜无眠,特此对本文翻译一部分,未完成部分待后续更新 本文源文地址 意欲使用 HSSF 和 XSSF 功能快熟读写电子表格?那本文就是为你而写的 ...
随机推荐
- Go语言 映射(map)
Go语言 映射(map) 1. 什么是 map2. 创建 map3. 访问 map4. nil map和空map5. map中元素的返回值6. len()和delete()7. 测试map中元素是否 ...
- linux mysql授权远程连接,创建用户等
1.进入mysql 2.此命令是为密码为 root .IP(%)任意的 root 用户授权.(*.* 表示数据库.表,to后为root用户:%:模糊查询,所有 IP 都可以,可指定其他主机 IP:by ...
- swagger不再是第一选择了
一.前言 工欲善其事,必先利其器 最近对 API 接口协作的软件研究了好久,市面上的软件都下载用了一轮,下面给大家介绍其中的最强「神器」 Apifox. Apifox 官网:apifox.cn 在 ...
- Mysql学习day1
安装了Mysql以及SQLyog,将SQLyog和数据库做了连接. 学习了基础数据类型以及命令行语句 1 alter table `student` rename as `stu``lesson` 2 ...
- go - 内存分配机制详解
一般程序的内存分配,从高位到低位依次为 全局静态区:用于存储全局变量.静态变量等:这部分内存在程序编译时已经分配好,由操作系统管理,速度快,不易出错. 栈:函数中的基础类型的局部变量:由程序进行系统调 ...
- gin框架使用【4.请求参数】
GET url: http://127.0.0.1:8080/users?id=1&name=卷毛狒狒 package mainimport ( "github.com/gin-go ...
- 【HarmonyOS学习笔记】记第一次使用IDE
哈喽大家好我是脸皮贼厚的小威 愚人节刚过先给大家拜个早年吧 最近在HarmonyOS官网下载了IDE,并抱着学(wan)习(wan)的心态试着跑出了Hello World,并安装到手机上 这是一个简单 ...
- 【深入理解TcaplusDB技术】扫描数据接口说明——[List表]
摘要 实现扫描指定表格中的数据. 示例代码 同步调用参见章节:[List表]扫描数据示例代码. 异步调用参见章节:[List表]异步扫描数据示例代码. Client对象方法说明 注:如有未列出来的Cl ...
- JS_简单的效果-鼠标移动、点击、定位元素、修改颜色等
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- 让交互更加生动!有意思的鼠标跟随 3D 旋转动效
今天,群友问了这样一个问题,如下所示的鼠标跟随交互效果,如何实现: 简单分析一下,这个交互效果主要有两个核心: 借助了 CSS 3D 的能力 元素的旋转需要和鼠标的移动相结合 本文,就将讲述如何使用纯 ...