packagecom.xfzx.test.POI.main;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.Map.Entry;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.apache.poi.POIXMLDocument;importorg.apache.poi.hwpf.HWPFDocument;importorg.apache.poi.hwpf.usermodel.Range;importorg.apache.poi.xwpf.usermodel.XWPFDocument;importorg.apache.poi.xwpf.usermodel.XWPFParagraph;importorg.apache.poi.xwpf.usermodel.XWPFRun;importorg.apache.poi.xwpf.usermodel.XWPFTable;importorg.apache.poi.xwpf.usermodel.XWPFTableCell;importorg.apache.poi.xwpf.usermodel.XWPFTableRow;public classWordPOI {//返回Docx中需要替换的特殊字符,没有重复项//推荐传入正则表达式参数"\\$\\{[^{}]+\\}"
public ArrayListgetReplaceElementsInWord(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= newFile(filePath);
HWPFDocument document= null;try{
InputStream is= newFileInputStream(file);
document= newHWPFDocument(is);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
<