Wrod文档内替换
test类
package com.tohi.digitization.srm.util;
import org.apache.poi.xwpf.usermodel.*;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordTest {
public void testReadByDoc() throws Exception {
InputStream is = new FileInputStream("D:\\cs.docx");
XWPFDocument doc = new XWPFDocument(is);
List<XWPFParagraph> paras = doc.getParagraphs();
for (XWPFParagraph para : paras) {
System.out.println(para.getText());
}
List<XWPFTable> tables = doc.getTables();
List<XWPFTableRow> rows;
List<XWPFTableCell> cells;
for (XWPFTable table : tables) {
rows = table.getRows();
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
System.out.println(cell.getText());;
}
}
}
this.close(is);
}
private void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void testTemplateWrite() throws Exception {
Map<String, Object> params = new HashMap<String, Object>();
String filePath = "D:\\cs.docx";
InputStream is = new FileInputStream(filePath);
XWPFDocument doc = new XWPFDocument(is);
HashMap<String, String> bookmark = new HashMap<String, String>();
bookmark.put("甲方盖章",