前段时间公司需要用poi导出word于是上网查了很多资料 运行到后面发现还是有问题的,这就上自己已经能正常运行的代码:
之前碰到了一个问题,就是表格中嵌套了表格 所以在遍历的时候就没有替换成功,现在已经可以了。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.xwpf.usermodel.*;
public class exportApplyForm {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Object> params = new HashMap<String, Object>();
params.put("name1", "哦");
params.put("name2", "啊");
params.put("bianma", "编码呀");
params.put("hzxm", "患者姓名咯");
XwpfTUtil xwpfTUtil = new XwpfTUtil();
XWPFDocument doc;
String fileNameInResource = "C:\\Users\\Administrator\\Desktop\\123\\123.docx";
InputStream is;
try {
InputStream is2 = new FileInputStream("C:\\Users\\Administrator\\Desktop\\123\\药学部专用报表1.docx");
is = new FileInputStream(fileNameInResource);
doc = new XWPFDocument(is2);
xwpfTUtil.replaceInPara(doc, params);
//替换表格里面的变量
xwpfTUtil.replaceInTable(doc, params);
OutputStream os = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\123\\write2.doc");
doc.write(os);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import org.apache.poi.xwpf.usermodel.*;
import java.io.*;
import java.util.ArrayList;
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 javax.security.auth.kerberos.KerberosKey;
public class XwpfTUtil {
public void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
XWPFParagraph para;
while (iterator.hasNext()) {
para = iterator.next();
this.replaceInPara(para, params);
}
}
public void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
List<XWPFRun> runs;
Matcher matcher;
runs = para.getRuns();
int start = -1;
int end = -1;
String str = "";
for (int i = 0; i < runs.size(); i++) {
XWPFRun run = runs.get(i);
String runText = run.toString();
if(runText.equals("SSB")){
System.out.println("getFirstLineIndent:"+para.getIndentationFirstLine());
}
if(!runText.equals("药 品 不 良 反 应 / 事 件 报 告 表")){
for (String key : params.keySet()) {
if(runText.indexOf(key)!=-1){
run.setFontFamily("宋体");
run.setFontSize(9);
runText = runText.replaceAll(key, (String) params.get(key));
run.setText(runText,0);
}
}
}
}
}
public void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFTable> iterator = doc.getTablesIterator();
XWPFTable table = null;
List<XWPFTableRow> rows;
List<XWPFTableCell> cells;
List<XWPFParagraph> paras;
while (iterator.hasNext()) {
table = iterator.next();
rows = table.getRows();
List<String[]> testList = new ArrayList<String[]>();
testList.add(new String[]{"1","1AA","1BB","1CC"});
testList.add(new String[]{"2","2AA","2BB","2CC"});
testList.add(new String[]{"3","3AA","3BB","3CC"});
testList.add(new String[]{"4","4AA","4BB","4CC"});
table.addRow(rows.get(4),5);
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
paras = cell.getParagraphs();
for (XWPFParagraph para : paras) {
this.replaceInPara(para, params);
}
}
}
}
}
public static void insertTable(XWPFTable table, List<String[]> tableList){
//创建行,根据需要插入的数据添加新行,不处理表头
table.addNewRowBetween(10, 10);
}
private Matcher matcher(String str) {
Pattern pattern = Pattern.compile("\\[(.+?)\\]", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
return matcher;
}
public void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}