博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用POI替换word中的特定字符/文字改进版
阅读量:6315 次
发布时间:2019-06-22

本文共 7625 字,大约阅读时间需要 25 分钟。

package com.xfzx.test.POI.main;    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.util.ArrayList;  import java.util.HashMap;  import java.util.Iterator;  import java.util.List;  import java.util.Map;  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.xwpf.usermodel.XWPFDocument;  import org.apache.poi.xwpf.usermodel.XWPFParagraph;  import org.apache.poi.xwpf.usermodel.XWPFRun;  import org.apache.poi.xwpf.usermodel.XWPFTable;  import org.apache.poi.xwpf.usermodel.XWPFTableCell;  import org.apache.poi.xwpf.usermodel.XWPFTableRow;    public class WordPOI {        // 返回Docx中需要替换的特殊字符,没有重复项      // 推荐传入正则表达式参数"\\$\\{[^{}]+\\}"      public ArrayList
getReplaceElementsInWord(String filePath, String regex) { String[] p = filePath.split("\\."); if (p.length > 0) {
// 判断文件有无扩展名 // 比较文件扩展名 if (p[p.length - 1].equalsIgnoreCase("doc")) { ArrayList
al = new ArrayList<>(); File file = new File(filePath); HWPFDocument document = null; try { InputStream is = new FileInputStream(file); document = new HWPFDocument(is); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Range range = document.getRange(); String rangeText = range.text(); CharSequence cs = rangeText.subSequence(0, rangeText.length()); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(cs); int startPosition = 0; while (matcher.find(startPosition)) { if (!al.contains(matcher.group())) { al.add(matcher.group()); } startPosition = matcher.end(); } return al; } else if (p[p.length - 1].equalsIgnoreCase("docx")) { ArrayList
al = new ArrayList<>(); XWPFDocument document = null; try { document = new XWPFDocument( POIXMLDocument.openPackage(filePath)); } catch (IOException e) { e.printStackTrace(); } // 遍历段落 Iterator
itPara = document .getParagraphsIterator(); while (itPara.hasNext()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next(); String paragraphString = paragraph.getText(); CharSequence cs = paragraphString.subSequence(0, paragraphString.length()); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(cs); int startPosition = 0; while (matcher.find(startPosition)) { if (!al.contains(matcher.group())) { al.add(matcher.group()); } startPosition = matcher.end(); } } // 遍历表 Iterator
itTable = document.getTablesIterator(); while (itTable.hasNext()) { XWPFTable table = (XWPFTable) itTable.next(); int rcount = table.getNumberOfRows(); for (int i = 0; i < rcount; i++) { XWPFTableRow row = table.getRow(i); List
cells = row.getTableCells(); for (XWPFTableCell cell : cells) { String cellText = ""; cellText = cell.getText(); CharSequence cs = cellText.subSequence(0, cellText.length()); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(cs); int startPosition = 0; while (matcher.find(startPosition)) { if (!al.contains(matcher.group())) { al.add(matcher.group()); } startPosition = matcher.end(); } } } } return al; } else { return null; } } else { return null; } } /* 何问起 hovertree.com */ // 替换word中需要替换的特殊字符 public static boolean replaceAndGenerateWord(String srcPath, String destPath, Map
map) { String[] sp = srcPath.split("\\."); String[] dp = destPath.split("\\."); if ((sp.length > 0) && (dp.length > 0)) { // 判断文件有无扩展名 // 比较文件扩展名 if (sp[sp.length - 1].equalsIgnoreCase("docx")) { try { XWPFDocument document = new XWPFDocument( POIXMLDocument.openPackage(srcPath)); // 替换段落中的指定文字 Iterator
itPara = document .getParagraphsIterator(); while (itPara.hasNext()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next(); List
runs = paragraph.getRuns(); for (int i = 0; i < runs.size(); i++) { String oneparaString = runs.get(i).getText( runs.get(i).getTextPosition()); for (Map.Entry
entry : map .entrySet()) { oneparaString = oneparaString.replace( entry.getKey(), entry.getValue()); } runs.get(i).setText(oneparaString, 0); } } // 替换表格中的指定文字 Iterator
itTable = document.getTablesIterator(); while (itTable.hasNext()) { XWPFTable table = (XWPFTable) itTable.next(); int rcount = table.getNumberOfRows(); for (int i = 0; i < rcount; i++) { XWPFTableRow row = table.getRow(i); List
cells = row.getTableCells(); for (XWPFTableCell cell : cells) { String cellTextString = cell.getText(); for (Entry
e : map.entrySet()) { if (cellTextString.contains(e.getKey())) cellTextString = cellTextString .replace(e.getKey(), e.getValue()); } cell.removeParagraph(0); cell.setText(cellTextString); } } } FileOutputStream outStream = null; outStream = new FileOutputStream(destPath); document.write(outStream); outStream.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } else // doc只能生成doc,如果生成docx会出错 if ((sp[sp.length - 1].equalsIgnoreCase("doc")) && (dp[dp.length - 1].equalsIgnoreCase("doc"))) { HWPFDocument document = null; try { document = new HWPFDocument(new FileInputStream(srcPath)); Range range = document.getRange(); for (Map.Entry
entry : map.entrySet()) { range.replaceText(entry.getKey(), entry.getValue()); } FileOutputStream outStream = null; outStream = new FileOutputStream(destPath); document.write(outStream); outStream.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } } else { return false; } } else { return false; } } public static void main(String[] args) { // TODO Auto-generated method stub String filepathString = "D:/2.doc"; String destpathString = "D:/2ttt.doc"; Map
map = new HashMap
(); map.put("${NAME}", "王五王五啊柯乐义的辣味回答侯何问起网"); map.put("${NsAME}", "王五王五啊王力味回答侯何问起网"); map.put("${NAMaE}", "王五王五啊柯乐义侯何问起网"); map.put("${NArME}", "王五王五啊柯乐义的辣味回答东拉网"); map.put("${NwAME}", "王五王五啊王的辣味回答侯何问起网"); map.put("${NA4ME}", "王五王五啊王力侯何问起网"); map.put("${N5AME}", "王五王五辣味回答侯何问起网"); map.put("${NAadwME}", "王五力宏的辣味回答侯何问起网"); System.out.println(replaceAndGenerateWord(filepathString, destpathString, map)); } }

推荐:

转载于:https://www.cnblogs.com/roucheng/p/POIword.html

你可能感兴趣的文章
SpringCloud:Eureka Client项目搭建(Gradle项目)
查看>>
jqueryValidate
查看>>
ATL使用IE控件,并且屏蔽右键
查看>>
Jenkins
查看>>
linux下使用screen和ping命令对网络质量进行监控
查看>>
数据库设计技巧
查看>>
css定位概述
查看>>
C# 动态修改配置文件 (二)
查看>>
BOM:文档对象模型 --树模型
查看>>
我的Android进阶之旅------>WindowManager.LayoutParams介绍
查看>>
segment
查看>>
获取鼠标的原始移动值
查看>>
Linux信号 编程
查看>>
有关滚动与位置
查看>>
Box2D自定义重力
查看>>
chpasswd
查看>>
mysqldump --single-transaction 和--lock-tables参数详解
查看>>
android 数据库_sql语句总结
查看>>
python购物车
查看>>
解决python2和python3的pip冲突
查看>>