在用POI进行excel表导出时,遇到中文文件名乱码问题,用下面的方法得到了解决。
转载自:https://my.oschina.net/chinamummy29/blog/525639
在导出前对名称根据浏览器做下处理
// 判断浏览器类型,firefox浏览器做特殊处理,否则下载文件名乱码
public static void compatibleFileName(HttpServletRequest request, HttpServletResponse response, String excelname) throws UnsupportedEncodingException {
String agent = request.getHeader("USER-AGENT").toLowerCase();
response.setContentType("application/vnd.ms-excel");
String fileName = excelname;
String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
if (agent.contains("firefox")) {
response.setCharacterEncoding("utf-8");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls");
} else {
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
}
}