以上数据为替换部分的数据。
以上为模版内容
/**
*
* @param params //需要替换的文字。
* @param wgylist1 //生成表格的list
* @param lzlist1 //生成表格的list
* @param dateStr
* @param fileName
* 读取模版内容,替换模版内的文字。并在模版后生成表格。
*/
private void download(Map
//根据文件的项目路径,获得文件的系统路径。
String path = ExportExcelHelper.class.getResource(“/template”+File.separatorChar+”statistics.docx”).getPath();
is = new FileInputStream(path);
doc = new XWPFDocument(is);
xwpfTUtil.replaceInPara(doc, params);
ExportWordHelper ewp=new ExportWordHelper();
ewp.ExportWord(wgylist1, lzlist1,doc,dateStr);
// uploadsDir为项目中配置的路径。
File dir = new File(uploadsDir+”/uploads/weekly”);
if (!dir.exists()) {// 判断文件目录是否存在
dir.mkdirs();
}
//下载文件到硬盘
FileOutputStream os = new FileOutputStream(uploadsDir+”/uploads/weekly/”+fileName+”.docx”);
doc.write(os);
xwpfTUtil.close(is);
xwpfTUtil.close(os);
} catch (Exception e) {
e.printStackTrace();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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;
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
String runText = run.toString().trim();
if(!””.equals(runText)){
if (‘$’ == runText.charAt(0)&&'{‘ == runText.charAt(1)) {
start = i;
}
if ((start != -1)) {
str += runText;
}
if (‘}’ == runText.charAt(runText.length() – 1)) {
if (start != -1) {
end = i;
break;
}
}
}
}
if(start!=-1){
for(int i=start;i
}
XWPFRun createRun = para.insertNewRun(start);
for (String key : params.keySet()) {
if (str.equals(key)) {
createRun.setText((String) params.get(key));
createRun.setFontSize(16);
createRun.addBreak();
break;
}
}
}
}
}
/**
* 正则匹配字符串
*
* @param str
* @return
*/
private Matcher matcher(String str) {
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();
}
}
}
}
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
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;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
public class ExportWordHelper {
//导出表格到word
/**
*
* @param statisticalGridman 中山区网格员上报案件排名
* @param totalGridman 有多少人没上报过案件
* @return
* @throws Exception
*/
public XWPFDocument ExportWord(List
//两个表格之间加个换行
XWPFParagraph paragraph = document.createParagraph();
XWPFRun paragraphRun = paragraph.createRun();
paragraphRun.setText(“\r”);
//添加标题,
XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText(dateStr+”此处为表格的标题部分”);
titleParagraphRun.setColor(“000000”);
titleParagraphRun.setFontSize(24);
titleParagraphRun.setFontFamily(“宋体”);
//换行
XWPFParagraph paragraph1 = document.createParagraph();
XWPFRun paragraphRun1 = paragraph1.createRun();
paragraphRun1.setText(“\r”);
//表格部分
XWPFTable ComTable = document.createTable();
//列宽自动分割
CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
comTableWidth.setType(STTblWidth.DXA);
comTableWidth.setW(BigInteger.valueOf(9072));
//表格第一行
XWPFTableRow comTableRowOne= ComTable.getRow(0);
comTableRowOne.getCell(0).setText(“序号”);
comTableRowOne.addNewTableCell().setText(“测试”);
comTableRowOne.addNewTableCell().setText(“测试”);
comTableRowOne.addNewTableCell().setText(“测试”);
comTableRowOne.addNewTableCell().setText(“测试”);
comTableRowOne.addNewTableCell().setText(“测试”);
comTableRowOne.addNewTableCell().setText(“测试”);
comTableRowOne.addNewTableCell().setText(“测试”);
for(int i=0;i
XWPFTableRow comTableRow = ComTable.createRow();
comTableRow.getCell(0).setText(“”+(i+1));
comTableRow.getCell(1).setText((String)map.get(“JD”));
comTableRow.getCell(2).setText((String)map.get(“SQ”));
comTableRow.getCell(3).setText((String)map.get(“NAME”));
comTableRow.getCell(4).setText(String.valueOf(map.get(“CODE”)));
comTableRow.getCell(5).setText(String.valueOf(map.get(“AJZS1”)));
comTableRow.getCell(6).setText(String.valueOf(map.get(“XCDWS1”)));
comTableRow.getCell(7).setText(String.valueOf(map.get(“XCCS1”)));
}
//换行
XWPFParagraph paragraph2 = document.createParagraph();
XWPFRun paragraphRun2 = paragraph2.createRun();
paragraphRun2.setText(“\r”);
/**
* 设置字体样式
* @param cell
* @param text
* @throws Exception
*/
public void setCellText(XWPFTableCell cell,String text) throws Exception{
XWPFParagraph cellP=cell.getParagraphs().get(0);
XWPFRun cellR = cellP.createRun();
cellR.setText(text);
cellR.setFontFamily(“宋体”);
cellR.setBold(true);
cellR.setFontSize(9);
}
/**
* 跨列合并 表格的下标是从0开始的
* @param table 操作的是哪个表格
* @param col 在第几行合并
* @param fromRow 在哪个列上开始合并
* @param toRow 在哪个列上结束合并
*/
public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
for (int i = fromRow; i <= toRow; i++) {
XWPFTableCell cell = table.getRow(i).getCell(col);
if ( i == fromRow ) {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
/**
* 跨行合并 表格的下标是从0开始的
* @param table 操作的是哪个表格
* @param row 在第几列合并
* @param fromCell 在哪个行上开始合并
* @param toCell 在哪个行上结束合并
*/
public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int i = fromCell; i <= toCell; i++) {
XWPFTableCell cell = table.getRow(row).getCell(i);
if ( i == fromCell ) {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
}
运行结果如下