日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

8、jeecg 笔记之 自定义word 模板导出(一)

發布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 8、jeecg 笔记之 自定义word 模板导出(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

8、jeecg 筆記之 自定義word 模板導出(一)

1、前言

jeecg 中已經自帶 word 的導出導出功能,其所使用的也是 easypoi,盡管所導出的 word 能滿足大部分需求,

但總是有需要用到自定義 word導出模板,下文所用到的皆是 easypoi 提供的,為方便下次翻閱,故記之。

?

2、代碼部分

2.1、controller

@RequestMapping("/ftl2word") public void velocity2word(JeecgDemoExcelEntity jeecgDemoExcel, HttpServletRequest request,HttpServletResponse response) throws IOException {try {jeecgDemoExcel = this.jeecgDemoExcelService.getEntity(JeecgDemoExcelEntity.class, jeecgDemoExcel.getId());List<Map<String, Object>> departs = this.systemService.findForJdbc("select id,departname from t_s_depart");String docFileName = "word-模板導出測試.doc";Map<String, Object> rootMap = new HashMap<String, Object>();rootMap.put("info", jeecgDemoExcel);rootMap.put("departs", departs);// FreemarkerUtil.createFile("exportMyExcel.xls",// docFileName,rootMap, request, response,// FreemarkerUtil.EXCEL_FILE);FreemarkerUtil.createFile("ftl2doc.ftl", docFileName, rootMap, request, response, FreemarkerUtil.WORD_FILE);} catch (Exception e) {e.printStackTrace();} }

?

2.2、entity

實體就不扔出來了,詳細說一下這個地方:

jeecgDemoExcel = this.jeecgDemoExcelService.getEntity(JeecgDemoExcelEntity.class, jeecgDemoExcel.getId()); List<Map<String, Object>> departs = this.systemService.findForJdbc("select id,departname from t_s_depart"); String docFileName = "word-模板導出測試.doc"; Map<String, Object> rootMap = new HashMap<String, Object>(); rootMap.put("info", jeecgDemoExcel); rootMap.put("departs", departs);

jeecgDemoExcel? 為 List<實體>,departs 為?List<Map<String, Object>>,怎么用?ftl 語法了解一下?

2.3、工具類?FreemarkerUtil?

public class FreemarkerUtil {private static final Object LOCK = new Object();/*** word文件*/public static final int WORD_FILE = 1;/*** excel文件*/public static final int EXCEL_FILE = 2;private static Configuration cfg;private static FreemarkerUtil ftl ;private FreemarkerUtil(String templateFolder) throws IOException {cfg = new Configuration();cfg.setDirectoryForTemplateLoading(new File(templateFolder));cfg.setObjectWrapper(new DefaultObjectWrapper());}private static void check(HttpServletRequest request) {if (ftl == null) {synchronized (LOCK) {try {ftl = new FreemarkerUtil(request.getServletContext().getRealPath("/")+"export/template");} catch (IOException e) {e.printStackTrace();}}}}/*** 創建 word 文檔* 必須先設置response導出配置,然后解析模版,否則會出問題* @throws IOException */public static void createFile(String templateName,String docFileName, Map<String,Object> rootMap,HttpServletRequest request, HttpServletResponse response,int fileType) throws IOException {// response.resetBuffer();//設置導出response.addHeader("Cache-Control","no-cache");response.setCharacterEncoding("UTF-8");if( WORD_FILE == fileType){response.setContentType("application/vnd.ms-word;charset=UTF-8");}else if(EXCEL_FILE == fileType){response.setContentType("application/octet-stream;charset=UTF-8");}else{response.setContentType("application/octet-stream");}String ua = request.getHeader("user-agent");ua = ua == null ? null : ua.toLowerCase();if(ua != null && (ua.indexOf("firefox") > 0 || ua.indexOf("safari")>0)){try {docFileName = new String(docFileName.getBytes(),"ISO8859-1");response.addHeader("Content-Disposition","attachment;filename=" + docFileName);} catch (Exception e) {}}else{try {docFileName = URLEncoder.encode(docFileName, "utf-8");response.addHeader("Content-Disposition","attachment;filename=" + docFileName);} catch (Exception e) {}}check(request);//解析模版Template temp = cfg.getTemplate(templateName, "UTF-8");PrintWriter write = response.getWriter();try {temp.process(rootMap, write);} catch (TemplateException e) {e.printStackTrace();}finally {if(write != null){write.flush();write.close();}}} }

?

2.4、ftl 模板

https://files.cnblogs.com/files/niceyoo/ftl2doc.rar

至于,ftl 如何生成,以及如何寫,可自定查詢,后面也會單獨文章補充。

?

?博客地址:http://www.cnblogs.com/niceyoo

?

posted @ 2018-12-17 13:33 niceyoo 閱讀(...) 評論(...) 編輯 收藏

總結

以上是生活随笔為你收集整理的8、jeecg 笔记之 自定义word 模板导出(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。