您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。