Java使用get請求接收List集合數(shù)據(jù)(json)并導出報表問題
前言
最近看了一下項目模塊,發(fā)現(xiàn)前同事寫的文件導出的功能很便捷(EasyExcel)。
研究了一下發(fā)現(xiàn)這個功能需求以后變更的可能很大(需求只是導出了一個空模板,沒有數(shù)據(jù)),所以預想著給這個功能完善一下,把數(shù)據(jù)也跟著導出來(畢竟客戶很可能要這么干)。
當前實現(xiàn)效果如下:
很明顯,之前導出的只是一個空模板,為了用戶可以便捷更改信息再上傳
話不多說,開始正題
一、實現(xiàn)分析
1、想導出的數(shù)據(jù)只是物品明細,數(shù)據(jù)量不大、信息相對不算私密,且用戶使用頻繁。所以使用get請求
2、get請求如何接收list集合數(shù)據(jù)參數(shù)呢,將List集合對象轉(zhuǎn)換為json字符串接收,通過后臺解析即可。(推薦)
3、也可以用@Requestbody注解接收對象,但感覺這樣可能不符合規(guī)范,畢竟@Requestbody多用于post請求(后臺能接到,但這種傳遞方式前臺可能不太方便)
二、Maven依賴(基于EasyExcel實現(xiàn))
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
三、后臺代碼
測試類:
@RestController @RequestMapping("excel/test") public class excelTest { @GetMapping("fileTest") public void downloadTemplate(@RequestParam String json, HttpServletResponse response) { String fileName = "測試導出模板"; String sheetName = "測試導出模板"; // 將json字符串轉(zhuǎn)換為List集合對象 List<GoodsDetailExcelEntity> list = JSONArray.parseArray(json, GoodsDetailExcelEntity.class); try { // 自定義的Excel工具類 ExcelUtil.writeExcel(response, list, fileName, sheetName, GoodsDetailExcelEntity.class); } catch (Exception e) { System.out.println(e.getCause()); } } }
GoodsDetailExcelEntity類
@Data public class GoodsDetailExcelEntity { @ExcelProperty(value = "物資", index = 0) private String goods; @ExcelProperty(value = "規(guī)格", index = 1) private String specs; @ExcelProperty(value = "單位", index = 2) private String unit; @ExcelProperty(value = "單價", index = 3) private BigDecimal price; @ExcelProperty(value = "數(shù)量", index = 4) private Integer number; @ExcelProperty(value = "金額", index = 5) private BigDecimal money; @ExcelProperty(value = "施工部位", index = 6) private String constructPosition; @ExcelProperty(value = "備注", index = 7) private String memo; }
ExcelUtil類,設(shè)置導出的excel樣式
/** * 導出 * @param response * @param data * @param fileName * @param sheetName * @param clazz * @throws Exception */ public static void writeExcel(HttpServletResponse response, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws Exception { //表頭樣式 WriteCellStyle headWriteCellStyle = new WriteCellStyle(); //設(shè)置表頭居中對齊 headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); //內(nèi)容樣式 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); //設(shè)置內(nèi)容靠左對齊 contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT); HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); EasyExcel.write(getOutputStream(fileName, response), clazz).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).registerWriteHandler(horizontalCellStyleStrategy).doWrite(data); } private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception { fileName = URLEncoder.encode(fileName, "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); response.addHeader("Access-Control-Expose-Headers", "Content-disposition"); return response.getOutputStream(); } }
四、使用PostMan測試
注意:因為是用get請求 所以{}和[]在postman中會被認為是特殊字符從而轉(zhuǎn)義
{ | %7B |
---|---|
} | %7D |
[ | %5B |
] | %5D |
不區(qū)分大小寫
因為是導出文件,所以選擇downLoad
接收到json格式的數(shù)據(jù)了,接下來就可以為所欲為了
程序跑完彈出窗口下載頁面(在瀏覽器下載)
生成的帶數(shù)據(jù)的Excel文件
總結(jié)
Excel樣式還是差點意思,大家可以自行設(shè)置哈。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于FastJson?long?溢出問題的小結(jié)
這篇文章主要介紹了關(guān)于FastJson?long?溢出問題的小結(jié),具有很好的參考價值,希望對大家有所幫助。2022-01-01關(guān)于SpringGateway調(diào)用服務 接受不到參數(shù)問題
這篇文章主要介紹了關(guān)于SpringGateway調(diào)用服務接受不到參數(shù)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12MyBatis Plus 將查詢結(jié)果封裝到指定實體的方法步驟
這篇文章主要介紹了MyBatis Plus 將查詢結(jié)果封裝到指定實體的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09使用JPA自定義VO接收返回結(jié)果集(unwrap)
這篇文章主要介紹了使用JPA自定義VO接收返回結(jié)果集(unwrap),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11