springMVC導(dǎo)出word模板的方法
本文實(shí)例為大家分享了springMVC導(dǎo)出word模板的具體代碼,供大家參考,具體內(nèi)容如下
controller 調(diào)用
@RequestMapping(value = "/exportWord") public void exportWord(HttpServletResponse response, HttpServletRequest request) throws IOException { String templatePath = request.getServletContext().getRealPath("") + "/template/稅源信息比對(duì).docx"; String fileName = new String("稅源信息比對(duì)".getBytes("gb2312"), "ISO8859-1") + ".docx"; /*數(shù)據(jù)*/ 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 參數(shù),導(dǎo)入的數(shù)據(jù) */ 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 參數(shù) */ 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 參數(shù) */ 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; } /** * 關(guān)閉輸入流 * * @param is */ public void close(InputStream is) { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 關(guān)閉輸出流 * * @param os */ public void close(OutputStream os) { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java用freemarker導(dǎo)出word實(shí)用示例
- java導(dǎo)出生成word的簡(jiǎn)單方法
- java使用poi讀取ppt文件和poi讀取excel、word示例
- java讀取word-excel-ppt文件代碼
- 實(shí)例講解Java讀取一般文本文件和word文檔的方法
- 一個(gè)JAVA小項(xiàng)目--Web應(yīng)用自動(dòng)生成Word
- 使用Java讀取Word文件的簡(jiǎn)單例子分享
- java使用Jsoup組件生成word文檔
- java/word+fusionchart生成圖表深入分析
- Java使用poi將word轉(zhuǎn)換為html
相關(guān)文章
Spring?Cloud?Eureka服務(wù)注冊(cè)中心入門流程分析
這篇文章主要介紹了Spring?Cloud?Eureka服務(wù)注冊(cè)中心入門流程分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06java開發(fā)中使用IDEA活動(dòng)模板快速增加注釋的方法
這篇文章主要介紹了java開發(fā)中使用IDEA活動(dòng)模板快速增加注釋,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12MybatisPlus3.3.0沒有MybatisPlusInterceptor類問題的解決方法
項(xiàng)目使用的是mybatis-plus-extension3.3.0依賴,然后在我使用分頁插件的時(shí)候,發(fā)現(xiàn)無法導(dǎo)入MybatisPlusInterceptor類所以本文給大家介紹了MybatisPlus3.3.0沒有MybatisPlusInterceptor類問題的解決方法,需要的朋友可以參考下2023-12-12Mybatis中使用updateBatch進(jìn)行批量更新
這篇文章主要介紹了Mybatis中使用updateBatch進(jìn)行批量更新的相關(guān)資料,有逐條更新,sql批量更新等,具體實(shí)例代碼大家參考下本文2018-04-04Lombok不生效,提示java:?找不到符號(hào)的解決方案
這篇文章主要介紹了Lombok不生效,提示java:?找不到符號(hào)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(1)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07Java Web實(shí)現(xiàn)登錄頁面驗(yàn)證碼驗(yàn)證功能
這篇文章主要介紹了Java Web登錄頁面驗(yàn)證碼驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12微信支付java版V3驗(yàn)證數(shù)據(jù)合法性(Deom)
這篇文章主要介紹了微信支付java版V3驗(yàn)證數(shù)據(jù)合法性(Deom)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Java實(shí)現(xiàn)讀取html文本內(nèi)容并按照格式導(dǎo)出到excel中
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取html文本提取相應(yīng)內(nèi)容按照格式導(dǎo)出到excel中,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02