生活随笔
收集整理的這篇文章主要介紹了
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 template language)是Word模板引擎,基于Microsoft Word模板和數(shù)據(jù)生成新的文檔。
Apache
POI不僅在上層封裝了易用的文檔API(文本、圖片、表格、頁眉、頁腳、圖表等),也可以在底層直接操作文檔XML結構,poi-tl正是一個基于Apache
POI的Word模板引擎,并且擁有著讓人喜悅的特性。
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ù)模型
根據(jù)官方文檔介紹,poi-tl具有以下優(yōu)點: A、支持動態(tài)填充文本、圖片、表格、列表、文檔 B、支持DOCX格式,所有的模板標簽都是以 {{
開頭,以 }}結尾,模板標簽可以出現(xiàn)在任何非文本框的位置,包括頁眉,頁腳,表格內(nèi)部等等。
C、poi-tl的一個核心特點是數(shù)據(jù)模型與樣式的分離,同樣的數(shù)據(jù)模型可以用來渲染各種不同樣式的模板。
D、文檔的樣式繼承模板標簽的樣式,即如果模板{{title}}是藍色微軟雅黑加粗四號字體,則替換后的文本也是藍色微軟雅黑加粗四號字體。
<dependency><groupId>com.deepoove
</groupId><artifactId>poi-tl
</artifactId><version>1.5.0
</version>
</dependency>
<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>
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
;
public class WordUtil {public static void downloadWord(OutputStream out
,String templatePath
, Map
<String, Object> paramMap
) {Long time
= new Date().getTime();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()){newFile
.getParentFile().mkdirs();}XWPFTemplate template
= XWPFTemplate
.compile(templatePath
).render(paramMap
);try {template
.write(out
);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
;
@RequestMapping("/auth/exportWord/")
@RestController
public class ExportWordController {@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"); Map
<String, Object> params
= new HashMap<>();params
.put("name", "張三");params
.put("position", "開發(fā)工程師");params
.put("entry_time", "2020-07-30");params
.put("province", "江蘇省");params
.put("city", "南京市");params
.put("picture", new PictureRenderData(120, 120, "D:\\cssTest\\square.png"));String templatePath
= "template/menzhen.docx";WordUtil
.downloadWord(response
.getOutputStream(), templatePath
, params
);}
}
SpringBoot 項目使用easypoi實現(xiàn)導出word功能
easypoi官方文檔:http://easypoi.mydoc.io/#category_49974
<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
;
public class WordUtil2 {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();}
}
注意區(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
;
@RequestMapping("/auth/exportWord/")
@RestController
public class ExportWordController {@RequestMapping("/exportUserWord2")public void exportUserWord2(HttpServletRequest request
,HttpServletResponse response
) throws IOException
{Map
<String, Object> params
= new HashMap<>();String templatePath
="static/template/user2.docx";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
);String temDir
="D:/mimi/"+File
.separator
+"file/word/"; ;Long time
= new Date().getTime();String formatSuffix
= ".docx";String fileName
= time
+ formatSuffix
;WordUtil2
.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)容還不錯,歡迎將生活随笔推薦給好友。