最近在做文件导出,查资料发现easyExcel的方法,
后台部分
@PostMapping(value = "exportData")
public void exportData (@RequestBody(required = true) Map<String, Object> map, HttpServletResponse response) throws Exception {
List<CommunityCorrectionsSubjectInjunction> list = JSONObject.parseArray(JSON.toJSONString(map.get("list")), CommunityCorrectionsSubjectInjunction.class);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), CommunityCorrectionsSubjectInjunction.class).sheet("模板").doWrite(list);
}
前端部分
async exportData(map) {
await this.$store.dispatch('exportData', map)
},
然后会报这样一个错
查资料说是编码问题,但页面编码确实是utf-8
从后台传过来的response就已经乱码了
对文件导出不太了解,想请明白的大佬解决一下