POI读取word时读取变量分段的全网最强解决方式

一、poi word关键字替换踩过的巨坑

案例:@加盟商}  把关键字替换成为南京海瀚软件科技有限公司

通过XWPFRun获取文本时,可能会被word分成了三段 @   加盟商  },这时无法识别一个完整的变量去做替换。

 

二、解决方式

1、不要在word中直接输入参数,通过在文本中写好复制粘贴过来。

        经过实践证明某些字段确实可以,但是还是会出现被分段。(wps去做变量时不分段率高一些)

2、将word另存为xml文件,在编辑器中将变量在一个<w:t></w:t>节点里完整的放在里面

        这种方式最终结果是可以实现的。但是在xml中找非常费神且容易改错,且文档量过大的时候,每一个都去改不太实际

3、使用代码的方式去做替换

  • 先把变量处理成完整的变量(采用从@开始符拼接到}结束,中间有很多判断逻辑,看代码)
				// 遍历获取段落中所有的runs
				List<XWPFRun> runs = paragraph.getRuns();
				// 合并逻辑
				for (int i = 0; i < runs.size(); i++) {
					String text0 = runs.get(i).getText(runs.get(i).getTextPosition());
					if (text0 != null&&text0.contains("@")) {
						int startIndex = text0.lastIndexOf("@");
						int endIndex = 1;
						if (startIndex != -1) {
							endIndex = text0.substring(startIndex).indexOf("}");
						}
						if (endIndex < 0) {
							// 记录分隔符中间跨越的runs数量,用于字符串拼接和替换
							int num = 0;
							int j = i+1 ;
							for (; j < runs.size(); j++) {
								String text1 = runs.get(j).getText(runs.get(j).getTextPosition());
								if (text1 != null && text1.contains("}")) {
									num = j - i;
									break;
								}
							}
							if (num != 0) {
								// num!=0说明找到了@@配对,需要替换
								StringBuilder newText = new StringBuilder();
								for (int s = i; s <= i + num; s++) {
									String text2 = runs.get(s).getText(runs.get(s).getTextPosition());
									String replaceText = text2;
									if(s==i&&text2.contains("@")&&text2.contains("}")) {
										newText.append(text2);
									}
									else if(s==i&&text2.contains("@")) {
										replaceText = text2.substring(0, text2.indexOf("@"));
										newText.append(text2.substring(text2.indexOf("@")));
									}
									else if (text2.contains("}")) {
										replaceText = text2.substring(replaceText.indexOf("}") + 1);
										newText.append(text2.substring(0, text2.indexOf("}") + 1));
									} else {
										replaceText = "";
										newText.append(text2);
									}
									runs.get(s).setText(replaceText, 0);
								}
								runs.get(i).setText(newText.toString(), 0);
								i = i - 1;
							}
						}
					}
				}

 

 

最后附上poi 处理 word,关键字替换文本、图片、插入excel表、转pdf的完整案例代码

  • generateWord(param, data, template, descpath); 替换
  • wordToPDF(shamName, sfileName, toFileName, readname, ht_attach_id); jacob方式worf转pdf
package com.agilecontrol.fair.ifc.gold.poi.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagra
  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java使用POI库可以实现对Excel文件的读取操作。具体的步骤如下: 1. 引用POI库。在Java项目中引入POI相关的jar包。 2. 创建文件输入流。使用FileInputStream类创建一个输入流对象,并指定要读取的Excel文件路径。 3. 创建工作簿对象。使用HSSFWorkbook类创建一个工作簿对象,将输入流作为参数传入。 4. 获取工作表。使用getSheetAt方法获取指定的工作表,可以通过工作表的索引或名称进行获取。 5. 获取行。使用getRow方法获取指定行的对象,行号作为参数传递给该方法。 6. 获取单元格。使用getCell方法获取指定单元格的对象,行号和列号作为参数传递给该方法。 7. 获取单元格的值。使用getStringCellValue方法获取单元格的值,将其赋给一个字符串变量。 8. 输出结果。使用System.out.println方法将获取到的数据打印出来。 需要注意的是,在读取Excel文件可能会出现FileNotFoundException和IOException异常,需要进行异常处理。同,在读取完成后,需要关闭输入流。 下面是一个示例代码,用于演示Java使用POI读取Excel文件: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelReader { public static void poiRead() { FileInputStream xlsStream = null; try { // 创建文件输入流 xlsStream = new FileInputStream(new File("C:\\Users\\itour\\Desktop\\poiTest.xls")); // 创建工作簿对象 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(xlsStream); // 获取工作表 HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0); // 获取行 HSSFRow row = sheetAt.getRow(0); // 获取单元格 HSSFCell cell = row.getCell(0); // 获取单元格的值 String cellValue = cell.getStringCellValue(); System.out.println("获取到的数据是:" + cellValue); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (xlsStream != null) { try { xlsStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } ``` 以上代码演示了如何使用POI读取Excel文件中第一个工作表的第一个单元格的值。你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值