word模板怎么利用springMVC进行导出?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
controller 调用
@RequestMapping(value = "/exportWord") public void exportWord(HttpServletResponse response, HttpServletRequest request) throws IOException { String templatePath = request.getServletContext().getRealPath("") + "/template/税源信息比对.docx"; String fileName = new String("税源信息比对".getBytes("gb2312"), "ISO8859-1") + ".docx"; /*数据*/ Map<String, Object> params = new HashMap<String, Object>(); params.put("${name}", "aaaa"); params.put("${sex}", "bbbb"); TempleWordUtil wordUtil = new TempleWordUtil(); XWPFDocument doc; InputStream is = new FileInputStream(templatePath); // is = getClass().getClassLoader().getResourceAsStream(templatePath); doc = new XWPFDocument(is); //只能使用.docx的 wordUtil.replaceInPara(doc, params); //替换表格里面的变量 wordUtil.replaceInTable(doc, params); OutputStream os = response.getOutputStream(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); doc.write(os); wordUtil.close(os); wordUtil.close(is); os.flush(); os.close(); }
TempleWordUtil 工具类
import org.apache.poi.xwpf.usermodel.*; import java.io.*; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 写入word工具类 * @author z * */ public class TempleWordUtil { /** * 替换段落里面的变量 * * @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(); 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; } } } for (int i = start; i <= end; i++) { para.removeRun(i); i--; end--; } 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模板怎么利用springMVC进行导出问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。