poi 更新word中的指定格式的 文字

来源:互联网 发布:游戏推广联盟源码 编辑:程序博客网 时间:2024/05/27 03:30

package org.apache.poi.xwpf.usermodel.examples;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 ReplaceDemo {
/**
* 将word文件中的指定的字符串替换成相应的文本

* @param filePath
* @param params
*            要替换的文本组成的map集合
* @throws Exception
*/
public static void updateDocx(String filePath, Map<String, String> params) throws Exception {
FileInputStream out = new FileInputStream(filePath);
XWPFDocument doc = new XWPFDocument(out);
// 获取所有的表格
List<XWPFTable> ts = doc.getTables();
// 获取所有的段落
List<XWPFParagraph> phs = doc.getParagraphs();
Iterator<XWPFTable> tsIts = ts.iterator();
Iterator<XWPFParagraph> phIts = phs.iterator();
// 遍历所有的表格,将表格中的所有的可替换文字替换
while (tsIts.hasNext()) {
XWPFTable nextTable = tsIts.next();
update(nextTable, params);
}
// 遍历所有的段落,将段落中可替换文本替换
while (phIts.hasNext()) {
XWPFParagraph nextPh = phIts.next();
update(nextPh, params);
}
doc.write(new FileOutputStream("D:/simple_1.docx"));
doc.close();
out.close();
}


// 将制定的段落更新替换
private static void update(XWPFParagraph nextPh, Map<String, String> params) {
List<XWPFRun> runs = nextPh.getRuns();
for (XWPFRun run : runs) {
replaceRun(run, params);
}
}


public static void replaceRun(XWPFRun run, Map<String, String> params) {
String str = run.text();
Matcher m = getMatcher(str);
while (m.find()) {
String requiredPlaceText = m.group();
for (Map.Entry<String, String> param : params.entrySet()) {
if (param.getKey().equals(requiredPlaceText)) {
str = str.replace(requiredPlaceText, param.getValue());
run.setText(str, 0);// 在后面添加0代表将之前的数值擦除掉
}
}
}
}


// 将制定的表格更新替换
public static void update(XWPFTable nextTable, Map<String, String> params) {
List<XWPFTableRow> rows = nextTable.getRows();
for (XWPFTableRow row : rows) {
replaceTableRow(row, params);
}
}


// 将表格中的制定的行进行替换
public static void replaceTableRow(XWPFTableRow rows, Map<String, String> params) {
List<XWPFTableCell> cells = rows.getTableCells();
for (XWPFTableCell cell : cells) {
replaceCell(cell, params);
}
}


// 替换每一个cell格子
public static void replaceCell(XWPFTableCell cell, Map<String, String> params) {
List<XWPFParagraph> tablePhs = cell.getParagraphs();
for (XWPFParagraph xwpfParagraph : tablePhs) {
update(xwpfParagraph, params);
}
}


public static Matcher getMatcher(String str) {
// ${.*}
Pattern compile = Pattern.compile("\\$\\{.*\\}");
return compile.matcher(str);


}


public static void main(String[] args) throws Exception {


Map<String, String> map = new HashMap<String, String>();
map.put("${d1}", "小李");
map.put("${d2}", "26");
map.put("${d3}", "孩子");
map.put("${d4}", "小王");
map.put("${d5}", "小陈");
map.put("${d6}", "小谢");
map.put("${d7}", "小刘");
map.put("${d8}", "河南开封");
map.put("${d9}", "123456789");
map.put("${d10}", "公子不思凡");
map.put("${d11}", "焚翅成灰终不悔,我以我蝶逆轮回 ");
updateDocx("D:/simple.docx", map);
}
}




//一下是测试文档,一定要保证 代码中的字符串编码方式和word中的处于英文输入模式,否则可能部分不能替换


你好,我是${d1},我今年${d2}岁。

我家有个小顽皮,她就是我家的小公主${d3}。

姓名

年龄

家庭住址

电话号码

${d4}

${d2}

${d8}

${d9}

${d5}

${d2}

${d8}

${d9}

${d6}

${d2}

${d8}

${d9}

${d7}

${d2}

${d8}

${d9}

 

 

 

红颜似水流不返,可怜${d10}

 

我心如竹盼君归,我身化海君不知

${d11}

问天下,谁能逆尘醒梦!你不能,我不能,众生皆在一梦中!

 

阅读全文
0 0
原创粉丝点击