Spring MVC中使用POI导出Word

内容绝大部分来源于网络

准备工作


  • 准备【XwpfTUtil】工具类(来源于网络)
  • 准备word模版

下载【XwpfTUtil】工具类

import org.apache.poi.xwpf.usermodel.*;

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; public class XwpfTUtil {
/**
* 替换段落里面的变量
*
* @param doc 要替换的文档
* @param params 参数
*/
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);
}
} /**
* 替换段落里面的变量
*
* @param para 要替换的段落
* @param params 参数
*/
public void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
List<XWPFRun> runs;
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();
System.out.println("------>>>>>>>>>" + 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;
}
}
}
System.out.println("start--->"+start);
System.out.println("end--->"+end); System.out.println("str---->>>" + str); for (int i = start; i <= end; i++) {
para.removeRun(i);
i--;
end--;
System.out.println("remove i="+i);
} for (String key : params.keySet()) {
if (str.equals(key)) {
para.createRun().setText((String) params.get(key));
break;
}
} }
} /**
* 替换表格里面的变量
*
* @param doc 要替换的文档
* @param params 参数
*/
public void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFTable> iterator = doc.getTablesIterator();
XWPFTable table;
List<XWPFTableRow> rows;
List<XWPFTableCell> cells;
List<XWPFParagraph> paras;
while (iterator.hasNext()) {
table = iterator.next();
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);
}
}
}
}
} /**
* 正则匹配字符串
*
* @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();
}
}
}
}

Word模版

需要填充的内容已${xxx}代替

Spring MVC中使用POI导出Word

完整步骤


一:页面中点击导出word按钮

//导出word
$("#导出按钮的ID").click(function () {
//请使用 window.location.href 发送请求。
window.location.href = "/exportfoWord"
});

二: 后台Controller

@RequestMapping("exportWord")
@ResponseBody
public String exportWord(HttpServletRequest request, HttpServletResponse response){
Service.exportWord(request,response);
return "success";
}

三:后台service

    public void exportWord(HttpServletRequest request, HttpServletResponse response) {

        XWPFDocument doc = null;
InputStream is=null;
OutputStream os = null;
XwpfTUtil xwpfTUtil = new XwpfTUtil();
try{
Map<String, Object> params = new HashMap<String, Object>();
params.put("${name}", "张三"); String fileNameInResource="temp.docx";//word模版的名字 is = getClass().getClassLoader().getResourceAsStream(fileNameInResource);//会在跟路径下面查看temp.doc文件。(web项目中为classes文件下面)
doc = new XWPFDocument(is);
xwpfTUtil.replaceInPara(doc, params);
//替换表格里面的变量
xwpfTUtil.replaceInTable(doc, params); os = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment;filename=export-Word-name.docx");//filename为导出的word的名字 doc.write(os); }catch (Exception e){
logger.debug(e.getMessage());
}finally {
xwpfTUtil.close(os);
xwpfTUtil.close(is);
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
} }
}

导出效果

Spring MVC中使用POI导出Word

上一篇:使用BeautifulSoup解析XML文档


下一篇:[测试题]wows