Excel导出不同浏览器下文件名乱码问题

解决思路:通过请求头中的User-Agent参数中的信息来区分不同浏览器

     public Object exportPz(HttpServletRequest request, HttpServletResponse response, Url url, UserDetails user,
Map<String, Object> urlParams, Map<String, String> reqParams) throws ParseException {
String json = "";
List<CAccvouchAndDetail> padlist = selectVoucherCommon(request, urlParams);
try {
HttpSession session = request.getSession();
CUserZt userztModel = redisCache.getObject(RedisKey.ZT_MODEL_LIST+":"+session.getAttribute(Constant.ZT_ID),CUserZt.class); // 获取工作簿
HSSFWorkbook workbook = getWorkbook(userztModel.getZtname(), request);
CellStyle style = workbook.createCellStyle();
CellStyle styleL = workbook.createCellStyle();
CellStyle styleR = workbook.createCellStyle(); Font cellFont = workbook.createFont();
cellFont.setFontHeightInPoints((short) 10);
cellFont.setFontName("宋体");
style.setFont(cellFont);
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
style.setBorderTop((short) 1);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
// 获取工作簿的sheet表
HSSFSheet sheet = workbook.getSheetAt(0); styleL.cloneStyleFrom(style);
styleR.cloneStyleFrom(style);
styleL.setAlignment(HSSFCellStyle.ALIGN_LEFT);
styleR.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 设置excel中的值
setValue(sheet, padlist, style, styleL, styleR); String strFileName = com.wukongxiaozi.framework.utils.DateUtil.formatDate(new Date(), "yyyyMMddhhmmss")
+ "_凭证信息" + ".xls";
/**
* 本次添加主要修复不同浏览器下导出凭证文件名乱码的问题;author ltao
*/ final String userAgent = request.getHeader("USER-AGENT");
String finalFileName = null; if (StringUtils.contains(userAgent, "Edge")) {
finalFileName = URLEncoder.encode(strFileName, "UTF8");
} else if (StringUtils.contains(userAgent, "MSIE")) {// IE浏览器
finalFileName = URLEncoder.encode(strFileName, "UTF8");
} else if (StringUtils.contains(userAgent, "Mozilla")) {// google,火狐浏览器
finalFileName = new String(strFileName.getBytes(), "ISO8859-1");
} else {
finalFileName = URLEncoder.encode(strFileName, "UTF8");// 其他浏览器
} response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;fileName=" + finalFileName);
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
LOG.e(e.toString());
}
return json;
}

参考文档:http://blog.csdn.net/tongxinxiao/article/details/43733881

上一篇:python定时执行任务的三种方式


下一篇:VS Code +node npm 调试 js