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


很明顯,之前導(dǎo)出的只是一個(gè)空模板,為了用戶可以便捷更改信息再上傳
話不多說(shuō),開(kāi)始正題
一、實(shí)現(xiàn)分析
1、想導(dǎo)出的數(shù)據(jù)只是物品明細(xì),數(shù)據(jù)量不大、信息相對(duì)不算私密,且用戶使用頻繁。所以使用get請(qǐng)求
2、get請(qǐng)求如何接收l(shuí)ist集合數(shù)據(jù)參數(shù)呢,將List集合對(duì)象轉(zhuǎn)換為json字符串接收,通過(guò)后臺(tái)解析即可。(推薦)
3、也可以用@Requestbody注解接收對(duì)象,但感覺(jué)這樣可能不符合規(guī)范,畢竟@Requestbody多用于post請(qǐng)求(后臺(tái)能接到,但這種傳遞方式前臺(tái)可能不太方便)
二、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>
三、后臺(tái)代碼
測(cè)試類:
@RestController
@RequestMapping("excel/test")
public class excelTest {
@GetMapping("fileTest")
public void downloadTemplate(@RequestParam String json, HttpServletResponse response) {
String fileName = "測(cè)試導(dǎo)出模板";
String sheetName = "測(cè)試導(dǎo)出模板";
// 將json字符串轉(zhuǎn)換為L(zhǎng)ist集合對(duì)象
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è)置表頭居中對(duì)齊
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//內(nèi)容樣式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//設(shè)置內(nèi)容靠左對(duì)齊
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測(cè)試
注意:因?yàn)槭怯胓et請(qǐng)求 所以{}和[]在postman中會(huì)被認(rèn)為是特殊字符從而轉(zhuǎn)義
| { | %7B |
|---|---|
| } | %7D |
| [ | %5B |
| ] | %5D |
不區(qū)分大小寫(xiě)

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

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

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

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

總結(jié)
Excel樣式還是差點(diǎn)意思,大家可以自行設(shè)置哈。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatisPlus實(shí)現(xiàn)倒序拼接字符串
這篇文章主要介紹了mybatisPlus實(shí)現(xiàn)倒序拼接字符串方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
springboot集成gzip和zip數(shù)據(jù)壓縮傳輸(適用大數(shù)據(jù)信息傳輸)
?在大數(shù)據(jù)量的傳輸中,壓縮數(shù)據(jù)后進(jìn)行傳輸可以一定程度的解決速度問(wèn)題,本文主要介紹了springboot集成gzip和zip數(shù)據(jù)壓縮傳輸,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Spring?Boot集成Elasticsearch全過(guò)程
這篇文章主要介紹了Spring?Boot集成Elasticsearch全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-07-07
Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫(kù)
本篇文章主要介紹了Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫(kù),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
Java中mapstruct mapper轉(zhuǎn)換器部分字段轉(zhuǎn)換無(wú)效的解決方案
在SpringBoot2.1.5項(xiàng)目中,使用MapStruct1.3.0.Final進(jìn)行實(shí)體類字段映射時(shí)遇到問(wèn)題,下面就來(lái)具體介紹一下,感興趣的可以了解一下2025-10-10
Java?Valhalla?Project項(xiàng)目介紹
這篇文章主要介紹了Java?Valhalla?Project項(xiàng)目介紹,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

