当前位置:网站首页>使用POI替换word中的特定字符/文字改进版
使用POI替换word中的特定字符/文字改进版
2022-07-15 17:53:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
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<String> getReplaceElementsInWord(String filePath,
String regex) {
String[] p = filePath.split("\\.");
if (p.length > 0) {// 判断文件有无扩展名
// 比较文件扩展名
if (p[p.length - 1].equalsIgnoreCase("doc")) {
ArrayList<String> 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<String> al = new ArrayList<>();
XWPFDocument document = null;
try {
document = new XWPFDocument(
POIXMLDocument.openPackage(filePath));
} catch (IOException e) {
e.printStackTrace();
}
// 遍历段落
Iterator<XWPFParagraph> 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<XWPFTable> 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<XWPFTableCell> 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<String, String> 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<XWPFParagraph> itPara = document
.getParagraphsIterator();
while (itPara.hasNext()) {
XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
List<XWPFRun> runs = paragraph.getRuns();
for (int i = 0; i < runs.size(); i++) {
String oneparaString = runs.get(i).getText(
runs.get(i).getTextPosition());
for (Map.Entry<String, String> entry : map
.entrySet()) {
oneparaString = oneparaString.replace(
entry.getKey(), entry.getValue());
}
runs.get(i).setText(oneparaString, 0);
}
}
// 替换表格中的指定文字
Iterator<XWPFTable> 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<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
String cellTextString = cell.getText();
for (Entry<String, String> 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<String, String> 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<String, String> map = new HashMap<String, String>();
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://javaforall.cn/120384.html原文链接:https://javaforall.cn
边栏推荐
- Is it safe to open an account online now? Want to know which are the top ten securities dealers in China?
- SAR Image: common distribution when fitting clutter
- Software architecture and design (IV) -- data flow architecture
- Today, another chip IPO came out of Shenzhen Huaqiangbei
- Torch in pytoch Analysis of nonzero() function
- 337. Looting · dynamic planning
- What security risks need to be considered in cloud platforms and infrastructure
- 745. Prefix and suffix search
- Unity iotar augmented reality tutorial
- [untitled] slow SQL analysis and optimization
猜你喜欢

竞赛·6116·计算布尔二叉树的值·递归

From March to June, after summary, more than 200 pages of true question notes and detailed explanations (including core test sites and 6 major factories)

Des dizaines de milliards de données compressées à 600 go, tdengine est installé sur la plateforme mobile d'énergie de GCL

Race · 6116 · calculate the value of Boolean binary tree · recursion

Is it true or false that blue collar workers are sleepy and live broadcasting is needed?

競賽·6116·計算布爾二叉樹的值·遞歸

"Smart factory" goes online, breaking the traditional factory digital transformation

Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql

(zero six) flask is OK if you have hands - configure static files

LDAP介绍
随机推荐
394.字符串解码·栈
Consumer start flash back
Data transmission: Practice of batch extraction of isomorphic and heterogeneous IP data sources
Torch in pytoch Repeat() function parsing
Compétition · 6116 · calcul de la valeur de l'arbre binaire booléen · récursion
Matlab calculates the integral of normal function, and the quantile corresponding to the integral
How to solve Oracle cross network disconnection forwarding?
Is it safe to open an account online now? Want to know which are the top ten securities dealers in China?
BigDecimal compare size
Today, another chip IPO came out of Shenzhen Huaqiangbei
Blue whale configuration framework
概率密度函数中形状参数和尺度参数的区别
@Repository @ [email protected] Understanding of annotations
From March to June, after summary, more than 200 pages of true question notes and detailed explanations (including core test sites and 6 major factories)
CTSI foundation reserve transparency report - July 2022
Tens of billions of data were compressed to 600gb, and tdengine was launched on GCL energy mobile energy platform
竞赛·6116·计算布尔二叉树的值·递归
One question per day · 1252 Number of odd cells · simulation optimization
What's the use of fftshift? Why does matlab need fftshift after FFT?
一家火锅店,凑了三个IPO