當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot freemarker导出word下载
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot freemarker导出word下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
freemarker詳解
FreeMarker是一款模板引擎: 即一種基于模板和要改變的數據, 并用來生成輸出文本(HTML網頁、電子郵件、配置文件、源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發產品的組件。
FreeMarker是免費的,基于Apache許可證2.0版本發布。其模板編寫為FreeMarker Template Language(FTL),屬于簡單、專用的語言。需要準備數據在真實編程語言中來顯示,比如數據庫查詢和業務運算, 之后模板顯示已經準備好的數據。在模板中,主要用于如何展現數據, 而在模板之外注意于要展示什么數據。
項目圖片
操作流程
準備doc模版
另存為xml文件
修改名為userList.ftl后拷貝文件到src/main/resources
如何修改userList.ftl
Eclipse下如何打開ftl文件
下載一個專門的ftl編輯器插件,然后在里面格式化;
或者設置ftl的編輯器為jsp的編輯器,用它做format,eclipse->windows->preferences
修改userList.ftl第一步
原來,注意rId4的位置
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"Target="word/document.xml" />現在
<#list list as user> <Relationship Id="rId${user_index}Png"Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"Target="word/document.xml" /> </#list>修改userList.ftl第二步
原來,注意rId4的位置,image1.png
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"Target="media/image1.png" />現在
<#list list as user> <Relationship Id="rId${user_index}Png"Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image${user_index}.png" /> </#list>修改userList.ftl第三步
原來
<w:tr><w:tblPrEx><w:tblBorders><w:top w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:left w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:bottom w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:right w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:insideH w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:insideV w:val="single" w:color="auto" w:sz="4" w:space="0" /></w:tblBorders><w:tblLayout w:type="fixed" /><w:tblCellMar><w:left w:w="108" w:type="dxa" /><w:right w:w="108" w:type="dxa" /></w:tblCellMar></w:tblPrEx><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:pStyle w:val="4" /><w:jc w:val="center" /><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:t>111</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:pStyle w:val="4" /><w:jc w:val="center" /><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:t>222</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:pStyle w:val="4" /><w:jc w:val="center" /><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:t>333</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:pict><v:shape id="_x0000_i1026" o:spt="75" alt="wx" type="#_x0000_t75" style="height:93.25pt;width:95.6pt;"filled="f" o:preferrelative="t" stroked="f" coordsize="21600,21600"><v:path /><v:fill on="f" focussize="0,0" /><v:stroke on="f" /><v:imagedata r:id="rId4" o:title="wx" /><o:lock v:ext="edit" aspectratio="t" /><w10:wrap type="none" /><w10:anchorlock /></v:shape></w:pict></w:r></w:p></w:tc></w:tr>現在
<#list list as user><w:tr><w:tblPrEx><w:tblBorders><w:top w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:left w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:bottom w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:right w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:insideH w:val="single" w:color="auto" w:sz="4" w:space="0" /><w:insideV w:val="single" w:color="auto" w:sz="4" w:space="0" /></w:tblBorders><w:tblLayout w:type="fixed" /><w:tblCellMar><w:left w:w="108" w:type="dxa" /><w:right w:w="108" w:type="dxa" /></w:tblCellMar></w:tblPrEx><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:pStyle w:val="4" /><w:jc w:val="center" /><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:t>${user.id}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:pStyle w:val="4" /><w:jc w:val="center" /><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:t>${user.name}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:pStyle w:val="4" /><w:jc w:val="center" /><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:t>${user.age}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa" /><w:vAlign w:val="top" /></w:tcPr><w:p><w:pPr><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-CN" /></w:rPr><w:pict><v:shape id="_x0000_i1026" o:spt="75" alt="${user.name}" type="#_x0000_t75"style="height:93.25pt;width:95.6pt;" filled="f" o:preferrelative="t" stroked="f"coordsize="21600,21600"><v:path /><v:fill on="f" focussize="0,0" /><v:stroke on="f" /><v:imagedata r:id="rId${user_index}Png" o:title="${user.name}" /><o:lock v:ext="edit" aspectratio="t" /><w10:wrap type="none" /><w10:anchorlock /></v:shape></w:pict></w:r></w:p></w:tc></w:tr></#list>修改userList.ftl第四步,關鍵是圖片
原來
<pkg:part pkg:name="/word/media/image1.png" pkg:contentType="image/png"><pkg:binaryData>iVBORw0KGgoAAAANSUhEUgAAAPEAAADrCAYAAAC4hnD0AAAgAElEQVR4nOx9B5wkZZn..... gxxYtZVEXKLEIEZJxCVKDHJAxP8PvwV4N/MWQn4AAAAASUVORK5CYII=</pkg:binaryData></pkg:part>現在
<#list list as user><pkg:part pkg:name="/word/media/image${user_index}.png" pkg:contentType="image/png"><pkg:binaryData>${user.headPortrait}</pkg:binaryData></pkg:part></#list>注意標簽閉合,image名稱對應
UserController.download方法
@Autowiredprivate FreeMarkerConfigurer freeMarkerConfigurer;private Base64 b64Encoder = new Base64();// 處理下載word文檔@RequestMapping("/download")public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {// 告訴瀏覽器用什么軟件可以打開此文件response.setHeader("content-Type", "application/msword");// 下載文件的默認名稱response.setHeader("Content-Disposition", "attachment;filename=xx.doc");freeMarkerConfigurer.getConfiguration().setClassForTemplateLoading(getClass(), "/");Template template = freeMarkerConfigurer.getConfiguration().getTemplate("userList.ftl");String webapp = request.getServletContext().getRealPath("/");List<User> list = userRepository.findAll();for (int i = 0; i < list.size(); i++) {User user = list.get(i);File file = new File(webapp, user.getHeadPortrait());FileInputStream fis = new FileInputStream(file);byte[] imgData = new byte[fis.available()];fis.read(imgData);fis.close();String headPortrait = b64Encoder.encodeAsString(imgData);user.setHeadPortrait(headPortrait);}Map<String, Object> root = new HashMap<String, Object>();root.put("list", list);template.process(root, new OutputStreamWriter(response.getOutputStream()));}user.jsp,添加的核心代碼
function headPortraitFormatter(value,row,index){if (value){return "[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-tlmwspiM-1581738030149)("+value+")]";} else {return "";} }<th data-options="field:'headPortrait',width:10,formatter:headPortraitFormatter">頭像</th><a data-url="downloadUser" href="javascript:void(0)" class="easyui-linkbutton c5" iconCls="icon-reload">word下載</a>如果感覺不錯記得點贊喲!!!
總結
以上是生活随笔為你收集整理的Spring Boot freemarker导出word下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++班级通讯录
- 下一篇: Pycharm 字体大小及背景颜色的设置