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


很明顯,之前導(dǎo)出的只是一個(gè)空模板,為了用戶可以便捷更改信息再上傳
話不多說,開始正題
一、實(shí)現(xiàn)分析
1、想導(dǎo)出的數(shù)據(jù)只是物品明細(xì),數(shù)據(jù)量不大、信息相對不算私密,且用戶使用頻繁。所以使用get請求
2、get請求如何接收list集合數(shù)據(jù)參數(shù)呢,將List集合對象轉(zhuǎn)換為json字符串接收,通過后臺解析即可。(推薦)
3、也可以用@Requestbody注解接收對象,但感覺這樣可能不符合規(guī)范,畢竟@Requestbody多用于post請求(后臺能接到,但這種傳遞方式前臺可能不太方便)
二、Maven依賴(基于EasyExcel實(shí)現(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 = "測試導(dǎo)出模板";
String sheetName = "測試導(dǎo)出模板";
// 將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 = "單價(jià)", 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è)置導(dǎo)出的excel樣式
/**
* 導(dǎo)出
* @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測試
注意:因?yàn)槭怯胓et請求 所以{}和[]在postman中會(huì)被認(rèn)為是特殊字符從而轉(zhuǎn)義
| { | %7B |
|---|---|
| } | %7D |
| [ | %5B |
| ] | %5D |
不區(qū)分大小寫

因?yàn)槭菍?dǎo)出文件,所以選擇downLoad

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

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

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

總結(jié)
Excel樣式還是差點(diǎn)意思,大家可以自行設(shè)置哈。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)計(jì)算器加法小程序(圖形化界面)
這篇文章主要介紹了Java實(shí)現(xiàn)圖形化界面的計(jì)算器加法小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Mybatis中輸入輸出映射與動(dòng)態(tài)Sql圖文詳解
這篇文章主要給大家介紹了關(guān)于Mybatis中輸入輸出映射與動(dòng)態(tài)Sql的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
關(guān)于FastJson?long?溢出問題的小結(jié)
這篇文章主要介紹了關(guān)于FastJson?long?溢出問題的小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。2022-01-01
關(guān)于SpringGateway調(diào)用服務(wù) 接受不到參數(shù)問題
這篇文章主要介紹了關(guān)于SpringGateway調(diào)用服務(wù)接受不到參數(shù)問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
MyBatis Plus 將查詢結(jié)果封裝到指定實(shí)體的方法步驟
這篇文章主要介紹了MyBatis Plus 將查詢結(jié)果封裝到指定實(shí)體的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
使用JPA自定義VO接收返回結(jié)果集(unwrap)
這篇文章主要介紹了使用JPA自定義VO接收返回結(jié)果集(unwrap),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring計(jì)時(shí)器StopWatch使用示例
這篇文章主要介紹了Spring計(jì)時(shí)器StopWatch使用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

