linux 导出 excel文件名乱码,excel导出,文件名称中文乱码问题 · 大腿的博客

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

记录一个excel导出遇见的中文名称的问题

接到项目需要有excel的导入导出功能,基于poi实现还是挺简单的,文件的导入导出都已经实现,但是导出给前台的文件名称总是乱码,一开始以为是我后台的问题,我找解决方法找了两天但是都不外乎就那几个。最后发现是前台的代码问题…

后台导出代码

controller接口1

2

3

4

5

6

7

8

9

10

11

12(value = "导出模板", notes = "导出模板")

@GetMapping(value = "export-template")

public void (HttpServletRequest request, HttpServletResponse response){

String fileName = "模板.xls";

// 设置excel的头

String[] title = {"姓名", "性别", "年龄"};

// 自定义的excel工具,创建工作簿。

WorkBook wb = ExcelUtil.createExcel(fileName, title);

// 导出流

ExcelUtil.binaryOut(request, response, wb, fileName);

}

这里重点在binaryOut()方法1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42public class ExcelUtil{

...

public static void binaryOut(HttpServletRequest request, HttpServletResponse response, WorkBook wb, String fileName){

try (OutputString os = response.getOutputStream()) {

// 设置响应头

setResponseHeader(request, response, fileName);

wb.write(os);

wb.close();

os.flush();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void setResponseHeader(HttpServletRequest request, HttpServletResponse response, String fileName){

String userAgent = request.getHeader("USER-AGENT");

String finalFileName = null;

try {

// 处理不同浏览器的文件名乱码问题

try {

if (org.apache.commons.lang3.StringUtils.contains.contains(userAgent, "MSIE")) {

// IE

finalFileName = URLEncoder.encode(fileName, "UTF8");

} else if (org.apache.commons.lang3.StringUtils.contains.contains(userAgent, "Mozilla")) {

// 火狐,谷歌

finalFileName = new String(fileName.getBytes(), "ISO-8859-1");

} else {

// 其他

finalFileName = URLEncoder.encode(fileName, "UTF8");

}

} catch (UnsupportedEncodingException ue) {

ue.printStackTrace();

}

response.setContentType("applicaction/vnd.ms-excel");

// 设置文件名

response.setHeader("Content-Disposition", "attachment; filename=" + finalFileName);

} catch (Exception e) {

e.printStackTrace();

}

}

...

}

至此,导出的功能完成了,并且在本地测试都成功,文件名也是预设中文名。但是前端调用接口输出的文件就不是我们的中文。问题出在了前端的处理方式上。

前端代码

前端一开始的处理方式1

2

3

4

5

6export(res) {

...

let blob = new Blob([res], {type: "application/vnd.ms-excel"});

let objectUrl = URL.createObjectURL(blob);

window.location.href = objectUrl;

}

res是请求后台得到的response。前端这里直接将response的流拿出来生成blob,再将blob转文件。这种方式下载的文件只有文件内容。如果代码不去手动设置文件名称的话,浏览器会自动给生成的文件一个序列号,后缀名是头里面设置的Contant-Type对应的后缀名。

代码使用1window.location.href = "host:port/export-template"

直接用浏览器打开下载的api就可以了。至于为什么浏览器会直接下载还有不知道。

b2aa7a8720b467704103ceb6e253cf80.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值