欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决导出Excel表的时候,中文文件名出现乱码的问题

程序员文章站 2024-02-08 14:28:58
...

我用的是safari浏览器,正确的实现方式如下:

String filename = "学生信息";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//重要点
String fileName = new String(filename.getBytes(), "iso8859-1") + dateFormat.format(new Date().getTime()) + ".xls";

response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setContentType("application/x-download");
response.flushBuffer();
response.setCharacterEncoding("UTF-8");

刚开始的时候我用的是如下方法:这个对我这边不适用

String filename = "学生信息";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//---------重要点-----------
String fileName = new String(filename.getBytes("gb2312"), "iso8859-1") + dateFormat.format(new Date().getTime()) + ".xls";

response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.setContentType("application/x-download");
response.flushBuffer();
response.setCharacterEncoding("UTF-8");

原项目中用的是:下载下来发现得到的文件名是对中文文件名转义之后的,也不适用

String filename = "学生信息";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//---------重要点-----------
String fileName = URLEncoder.encode(filename,"utf-8") + dateFormat.format(new Date().getTime()) + ".xls";

response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setContentType("application/x-download");
response.flushBuffer();
response.setCharacterEncoding("UTF-8");