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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

SSM项目开发实战踩坑

發(fā)布時間:2025/3/12 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SSM项目开发实战踩坑 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • SSM/Springboot實現(xiàn)導出docx文檔
    • SSM+layui實現(xiàn)數(shù)據(jù)表格

SSM/Springboot實現(xiàn)導出docx文檔

  • 項目使用poi-tl實現(xiàn)導出word功能:獲取數(shù)據(jù)錄入到固定的word模板,最后生成新的word文檔。

    Poi-tl官方文檔:http://deepoove.com/poi-tl/

    • poi-tl介紹

    poi-tl(poi template language)是Word模板引擎,基于Microsoft Word模板和數(shù)據(jù)生成新的文檔。
    Apache
    POI不僅在上層封裝了易用的文檔API(文本、圖片、表格、頁眉、頁腳、圖表等),也可以在底層直接操作文檔XML結構,poi-tl正是一個基于Apache
    POI的Word模板引擎,并且擁有著讓人喜悅的特性。

    • poi-tl語法:總共就5種語法:

    1.文本模板{{var}} 渲染為 TextRenderData 或 String 數(shù)據(jù)模型

    2.圖片模板{{@photo}} 渲染為 PictureRenderData 數(shù)據(jù)模型

    3.表格模板{{#var}} 渲染數(shù)據(jù)模型是 MiniTableRenderData

    4.列表模板{{*var}} 渲染為 NumbericRenderData 數(shù)據(jù)模型

    5.文檔模板{{+var}} 渲染為 DocxRenderData 數(shù)據(jù)模型

    • poi-tl的優(yōu)點

    根據(jù)官方文檔介紹,poi-tl具有以下優(yōu)點: A、支持動態(tài)填充文本、圖片、表格、列表、文檔 B、支持DOCX格式,所有的模板標簽都是以 {{
    開頭,以 }}結尾,模板標簽可以出現(xiàn)在任何非文本框的位置,包括頁眉,頁腳,表格內(nèi)部等等。
    C、poi-tl的一個核心特點是數(shù)據(jù)模型與樣式的分離,同樣的數(shù)據(jù)模型可以用來渲染各種不同樣式的模板。
    D、文檔的樣式繼承模板標簽的樣式,即如果模板{{title}}是藍色微軟雅黑加粗四號字體,則替換后的文本也是藍色微軟雅黑加粗四號字體。

    • 具體使用
      • 引入依賴
    <!-- word導出 --> <!-- poi-tl是基于Apache POI的Word模板引擎。poi-tl依賴的是poi3.16版本 --> <dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.5.0</version> </dependency><!-- 對JSP的支持 --> <dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId> </dependency> <dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId> </dependency>
    • WordUtil 工具類
    import com.deepoove.poi.XWPFTemplate; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Date; import java.util.Map;import javax.servlet.http.HttpServletRequest;/*** word工具類* Poi-tl模板引擎官方文檔:http://deepoove.com/poi-tl/*/ public class WordUtil {/*** 根據(jù)模板填充內(nèi)容生成word,并下載* @param templatePath word模板文件路徑* @param paramMap 替換的參數(shù)集合*/public static void downloadWord(OutputStream out,String templatePath, Map<String, Object> paramMap) {Long time = new Date().getTime();// 生成的word格式String formatSuffix = ".docx";// 拼接后的文件名String fileName = time + formatSuffix;//設置生成的文件存放路徑,可以存放在你想要指定的路徑里面,臨時存放文件,用瀏覽器下載存放到瀏覽器下載路徑String rootPath="D:/mimi/"+File.separator+"file/word/"; String filePath = rootPath+fileName;File newFile = new File(filePath);//判斷目標文件所在目錄是否存在if(!newFile.getParentFile().exists()){//如果目標文件所在的目錄不存在,則創(chuàng)建父目錄newFile.getParentFile().mkdirs();}// 讀取模板templatePath并將paramMap的內(nèi)容填充進模板,即編輯模板(compile)+渲染數(shù)據(jù)(render)XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramMap);try {//out = new FileOutputStream(filePath);//輸出路徑(下載到指定路徑)// 將填充之后的模板寫入filePathtemplate.write(out);//將template寫到OutputStream中out.flush();out.close();template.close();} catch (Exception e) {e.printStackTrace();}}}
    • 創(chuàng)建模板,把模板docx文件放到項目某個文件夾下

    • 編寫一個controller 類,導出員工信息的接口類,供頁面請求
    import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map;/*** 導出Word* @author Administrator**/ @RequestMapping("/auth/exportWord/") @RestController public class ExportWordController {/*** 用戶信息導出word* @throws IOException */@RequestMapping("/exportUserWord")public void exportUserWord(HttpServletRequest request,HttpServletResponse response) throws IOException{response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" +new SimpleDateFormat("yyyyMMddHHmm").format(new Date())+".docx"); //導出的docx文件的文件名,以日期的形式Map<String, Object> params = new HashMap<>();// 以靜態(tài)數(shù)據(jù),渲染文本,可通過對象動態(tài)獲取params.put("name", "張三");params.put("position", "開發(fā)工程師");params.put("entry_time", "2020-07-30");params.put("province", "江蘇省");params.put("city", "南京市");// 渲染圖片,傳入本地圖片,要想傳入網(wǎng)上圖片或Java圖片可參考POI官網(wǎng)params.put("picture", new PictureRenderData(120, 120, "D:\\cssTest\\square.png"));// TODO 渲染其他類型的數(shù)據(jù)請參考官方文檔String templatePath = "template/menzhen.docx";//模板文件位置WordUtil.downloadWord(response.getOutputStream(), templatePath, params);} }
  • SpringBoot 項目使用easypoi實現(xiàn)導出word功能
    easypoi官方文檔:http://easypoi.mydoc.io/#category_49974
    • 引入easypoi依賴
    <!-- word導出 方式二:easypoi--> <dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.0.3</version> </dependency> <dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.0.3</version> </dependency> <dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.0.3</version> </dependency>
    • 封裝工具類
    import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.util.Map;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.util.Assert;import cn.afterturn.easypoi.word.WordExportUtil;/*** word工具類* easypoi官方文檔:http://easypoi.mydoc.io/#category_49974*/ public class WordUtil2 {/*** 導出word* <p>第一步生成替換后的word文件,只支持docx</p>* <p>第二步下載生成的文件</p>* <p>第三步刪除生成的臨時文件</p>* 模版變量中變量格式:{{foo}}* @param templatePath word模板地址* @param temDir 生成臨時文件存放地址* @param fileName 文件名* @param params 替換的參數(shù)* @param request HttpServletRequest* @param response HttpServletResponse*/public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {Assert.notNull(templatePath,"模板路徑不能為空");Assert.notNull(temDir,"臨時文件路徑不能為空");Assert.notNull(fileName,"導出文件名不能為空");Assert.isTrue(fileName.endsWith(".docx"),"word導出請使用docx格式");if (!temDir.endsWith("/")){temDir = temDir + File.separator;}File dir = new File(temDir);if (!dir.exists()) {dir.mkdirs();}try {String userAgent = request.getHeader("user-agent").toLowerCase();if (userAgent.contains("msie") || userAgent.contains("like gecko")) {fileName = URLEncoder.encode(fileName, "UTF-8");} else {fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");}XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);String tmpPath = temDir + fileName;FileOutputStream fos = new FileOutputStream(tmpPath);doc.write(fos);// 設置強制下載不打開response.setContentType("application/force-download");// 設置文件名response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);OutputStream out = response.getOutputStream();doc.write(out);out.close();} catch (Exception e) {e.printStackTrace();} finally {delFileWord(temDir,fileName);//這一步看具體需求,要不要刪}}/*** 刪除零時生成的文件*/public static void delFileWord(String filePath, String fileName) {File file = new File(filePath + fileName);File file1 = new File(filePath);file.delete();file1.delete();} }
    • 創(chuàng)建模板

    注意區(qū)分: easypoi:word模板的文本格式填充項 是 {{}},圖片也是 {{}}
    poi-tl:word模板的文本格式填充項是{{}},而圖片是 {{@}}

    • 創(chuàng)建Controller進行調(diào)用
    import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import com.deepoove.poi.data.PictureRenderData; import com.example.mimiprogram.common.WordUtil; import com.example.mimiprogram.common.WordUtil2;import cn.afterturn.easypoi.word.entity.WordImageEntity;/*** 導出Word* @author Administrator**/ @RequestMapping("/auth/exportWord/") @RestController public class ExportWordController {/*** 用戶信息導出word --- easypoi* @throws IOException */@RequestMapping("/exportUserWord2")public void exportUserWord2(HttpServletRequest request,HttpServletResponse response) throws IOException{Map<String, Object> params = new HashMap<>();/*//如果word模板地址放在 webapp下采用如下方式獲取word模板路徑://表示到項目的根目錄(webapp)下,要是想到目錄下的子文件夾,修改"/"即可String path = request.getSession().getServletContext().getRealPath("/");*///word模板地址放在src/main/resources/下,因為配置過靜態(tài)資源映射,所以采用如下方式獲取項目中的word模板地址:String templatePath ="static/template/user2.docx";//word模板地址// 渲染文本params.put("name", "張三");params.put("position", "開發(fā)工程師");params.put("entry_time", "2020-07-30");params.put("province", "江蘇省");params.put("city", "南京市");// 渲染圖片WordImageEntity image = new WordImageEntity();image.setHeight(120);image.setWidth(120);image.setUrl("D:\\cssTest\\square.png");image.setType(WordImageEntity.URL);params.put("picture", image);// TODO 渲染其他類型的數(shù)據(jù)請參考官方文檔String temDir="D:/mimi/"+File.separator+"file/word/"; ;//生成臨時文件存放地址//生成文件名Long time = new Date().getTime();// 生成的word格式String formatSuffix = ".docx";// 拼接后的文件名String fileName = time + formatSuffix;//文件名 帶后綴//導出wordWordUtil2.exportWord(templatePath, temDir, fileName, params, request, response);} }

    需要注意工具類WordUtil中:
    downloadWord方法中的一行代碼:

    out = new FileOutputStream(filePath);//輸出路徑(下載到指定路徑)

    1 exportUserWord方法中的兩行代碼:

    //瀏覽器下載

    response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" +new SimpleDateFormat("yyyyMMddHHmm").format(new Date())+".docx");

    如果想要顯示瀏覽器下載,exportUserWord方法中的這兩行代碼必須顯示,而downloadWord方法中的那一行代碼必須隱藏。

    如果想要下載到指定的路徑,exportUserWord方法中的這兩行代碼必須隱藏,而downloadWord方法中的那一行代碼必須顯示。

    SSM+layui實現(xiàn)數(shù)據(jù)表格

    SSM/Springboot+layui實現(xiàn)數(shù)據(jù)表格

    總結

    以上是生活随笔為你收集整理的SSM项目开发实战踩坑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。