Java使用get請求接收List集合數(shù)據(json)并導出報表問題
前言
最近看了一下項目模塊,發(fā)現(xiàn)前同事寫的文件導出的功能很便捷(EasyExcel)。
研究了一下發(fā)現(xiàn)這個功能需求以后變更的可能很大(需求只是導出了一個空模板,沒有數(shù)據),所以預想著給這個功能完善一下,把數(shù)據也跟著導出來(畢竟客戶很可能要這么干)。
當前實現(xiàn)效果如下:


很明顯,之前導出的只是一個空模板,為了用戶可以便捷更改信息再上傳
話不多說,開始正題
一、實現(xiàn)分析
1、想導出的數(shù)據只是物品明細,數(shù)據量不大、信息相對不算私密,且用戶使用頻繁。所以使用get請求
2、get請求如何接收list集合數(shù)據參數(shù)呢,將List集合對象轉換為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字符串轉換為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類,設置導出的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();
//設置表頭居中對齊
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//內容樣式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//設置內容靠左對齊
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中會被認為是特殊字符從而轉義
| { | %7B |
|---|---|
| } | %7D |
| [ | %5B |
| ] | %5D |
不區(qū)分大小寫

因為是導出文件,所以選擇downLoad

接收到json格式的數(shù)據了,接下來就可以為所欲為了

程序跑完彈出窗口下載頁面(在瀏覽器下載)

生成的帶數(shù)據的Excel文件

總結
Excel樣式還是差點意思,大家可以自行設置哈。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springboot集成gzip和zip數(shù)據壓縮傳輸(適用大數(shù)據信息傳輸)
?在大數(shù)據量的傳輸中,壓縮數(shù)據后進行傳輸可以一定程度的解決速度問題,本文主要介紹了springboot集成gzip和zip數(shù)據壓縮傳輸,具有一定的參考價值,感興趣的可以了解一下2023-09-09
Java用 Rhino/Nashorn 代替第三方 JSON 轉換庫
本篇文章主要介紹了Java用 Rhino/Nashorn 代替第三方 JSON 轉換庫,非常具有實用價值,需要的朋友可以參考下2017-05-05
Java中mapstruct mapper轉換器部分字段轉換無效的解決方案
在SpringBoot2.1.5項目中,使用MapStruct1.3.0.Final進行實體類字段映射時遇到問題,下面就來具體介紹一下,感興趣的可以了解一下2025-10-10

