Java使用get请求接收List集合数据(json)并导出报表问题
作者:醉梦年华
这篇文章主要介绍了Java使用get请求接收List集合数据(json)并导出报表问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
前言
最近看了一下项目模块,发现前同事写的文件导出的功能很便捷(EasyExcel)。
研究了一下发现这个功能需求以后变更的可能很大(需求只是导出了一个空模板,没有数据),所以预想着给这个功能完善一下,把数据也跟着导出来(毕竟客户很可能要这么干)。
当前实现效果如下:


很明显,之前导出的只是一个空模板,为了用户可以便捷更改信息再上传
话不多说,开始正题
一、实现分析
1、想导出的数据只是物品明细,数据量不大、信息相对不算私密,且用户使用频繁。所以使用get请求
2、get请求如何接收list集合数据参数呢,将List集合对象转换为json字符串接收,通过后台解析即可。(推荐)
3、也可以用@Requestbody注解接收对象,但感觉这样可能不符合规范,毕竟@Requestbody多用于post请求(后台能接到,但这种传递方式前台可能不太方便)
二、Maven依赖(基于EasyExcel实现)
<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 = "规格", index = 1)
private String specs;
@ExcelProperty(value = "单位", index = 2)
private String unit;
@ExcelProperty(value = "单价", index = 3)
private BigDecimal price;
@ExcelProperty(value = "数量", 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 |
不区分大小写

因为是导出文件,所以选择downLoad

接收到json格式的数据了,接下来就可以为所欲为了

程序跑完弹出窗口下载页面(在浏览器下载)

生成的带数据的Excel文件

总结
Excel样式还是差点意思,大家可以自行设置哈。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
