前段时间公司需要用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 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map
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();
}
/*HWPFDocument doc = new HWPFDocument(is);
*
*/
}
/**
* 关闭输入流
* @param is
*/
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
* @param os
*/
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import org.apache.poi.xwpf.usermodel.*;
/*import org.junit.Test; */
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 {
/**
* 替换段落里面的变量
*
* @param doc 要替换的文档
* @param params 参数
*/
public void replaceInPara(XWPFDocument doc, Map
Iterator
XWPFParagraph para;
while (iterator.hasNext()) {
para = iterator.next();
this.replaceInPara(para, params);
}
}
/**
* 替换段落里面的变量
*
* @param para 要替换的段落
* @param params 参数
*/
public void replaceInPara(XWPFParagraph para, Map
List
Matcher matcher;
/*if (this.matcher(para.getParagraphText()).find()) { */
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);
}
}
}
}
/* for (String key : params.keySet()) {
if (str.equals(key)) {
System.out.println("runs.size():"+runs.size());
for(XWPFRun r:runs){
sysout
}
XWPFRun run1 = runs.get(0);
XWPFRun run1 = para.createRun();
run1.setFontFamily("宋体");
run1.setFontSize(9);
run1.setText((String) params.get(key));
para.createRun().setFontSize(7);
para.createRun().setText("呵呵呵"+(String) params.get(key));
for(XWPFRun run1:runs){
run1.setFontSize(9);
run1.setText((String) params.get(key),0);
}
}
} */
/*} */
}
/**
* 替换表格里面的变量
*
* @param doc 要替换的文档
* @param params 参数
*/
public void replaceInTable(XWPFDocument doc, Map
Iterator
XWPFTable table = null;
List
List
List
/* while (iterator.hasNext()) {
table = iterator.next();
table.createRow();
XWPFTableRow newRow = table.getRow(0);
table.addRow(newRow,1);
newRow.getCell(0).setText("11");
List
for(int j = 0; j < cells1.size(); j++){
XWPFTableCell cell = cells1.get(j);
cell.setText("1");
}
rows = table.getRows();
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
paras = cell.getParagraphs();
for (XWPFParagraph para : paras) {
this.replaceInPara(para, params);
}
}
}
}*/
while (iterator.hasNext()) {
table = iterator.next();
rows = table.getRows();
List
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.addNewRowBetween(0, 1);
XWPFTableRow roww=table.getRow(1);
//设置单元格内容
roww.getCell(0).setText("11111");*/
table.addRow(rows.get(4),5);
/*insertTable(table, testList);*/
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
paras = cell.getParagraphs();
for (XWPFParagraph para : paras) {
this.replaceInPara(para, params);
/*List
runs = para.getRuns();
for (int i = 0; i < runs.size(); i++) {
XWPFRun run = runs.get(i);
String runText = run.toString();
if(runText.equals("SSB")){
table.addRow(row,5);
}
}*/
}
}
}
}
}
/**
* 为表格插入数据,行数不够添加新行
* @param table 需要插入数据的表格
* @param tableList 插入数据集合
*/
public static void insertTable(XWPFTable table, List
//创建行,根据需要插入的数据添加新行,不处理表头
/*for(int i = 1; i < tableList.size(); i++){
XWPFTableRow row =table.createRow();
}*/
/*for(int i = 1; i < 2; i++){
XWPFTableRow row =table.createRow();
}
//遍历表格插入数据
List
for(int i = 1; i < rows.size(); i++){
XWPFTableRow newRow = table.getRow(rows.size()-1);
List
for(int j = 0; j < cells.size(); j++){
XWPFTableCell cell = cells.get(j);
cell.setText("1");
}*/
table.addNewRowBetween(10, 10);
}
/*}*/
/**
* 正则匹配字符串
*
* @param str
* @return
*/
private Matcher matcher(String str) {
/*Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);*/
Pattern pattern = Pattern.compile("\\[(.+?)\\]", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
return matcher;
}
/**
* 关闭输入流
*
* @param is
*/
public void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
*
* @param os
*/
public void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}