乱码主要是因为httpServletResponse的header编码问题,
但是,不同浏览器编码又不同,
经过测试,使用一下方法解决:
设置Response头信息编码
if ("IE".equals(getBrowser(request))) {
fileName = new String(java.net.URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
}else{
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
}
/**
* 判断客户端浏览器类型
* @param request
* @return
*/
private String getBrowser(HttpServletRequest request) {
String UserAgent = request.getHeader("User-Agent").toLowerCase();
if (UserAgent.indexOf("firefox") >= 0){
return "FF";
}else if(UserAgent.indexOf("safari") >= 0 ){
return "Chrome";
}else{
return "IE";
}
}
经过以上设置,并验证了谷歌、火狐、搜狗、360、IE这几个浏览器,导出excel的文件名均无乱码