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


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

 

 1     public Object exportPz(HttpServletRequest request, HttpServletResponse response, Url url, UserDetails user,
 2             Map<String, Object> urlParams, Map<String, String> reqParams) throws ParseException {
 3         String json = "";
 4         List<CAccvouchAndDetail> padlist = selectVoucherCommon(request, urlParams);
 5         try {
 6             HttpSession session = request.getSession();
 7             CUserZt userztModel = redisCache.getObject(RedisKey.ZT_MODEL_LIST+":"+session.getAttribute(Constant.ZT_ID),CUserZt.class);
 8             
 9 
10             // 获取工作簿
11             HSSFWorkbook workbook = getWorkbook(userztModel.getZtname(), request);
12             CellStyle style = workbook.createCellStyle();
13             CellStyle styleL = workbook.createCellStyle();
14             CellStyle styleR = workbook.createCellStyle();
15 
16             Font cellFont = workbook.createFont();
17             cellFont.setFontHeightInPoints((short) 10);
18             cellFont.setFontName("宋体");
19             style.setFont(cellFont);
20             style.setBorderBottom((short) 1);
21             style.setBorderLeft((short) 1);
22             style.setBorderRight((short) 1);
23             style.setBorderTop((short) 1);
24             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
25             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
26             // 获取工作簿的sheet表
27             HSSFSheet sheet = workbook.getSheetAt(0);
28 
29             styleL.cloneStyleFrom(style);
30             styleR.cloneStyleFrom(style);
31             styleL.setAlignment(HSSFCellStyle.ALIGN_LEFT);
32             styleR.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
33 
34             // 设置excel中的值
35             setValue(sheet, padlist, style, styleL, styleR);
36 
37             String strFileName = com.wukongxiaozi.framework.utils.DateUtil.formatDate(new Date(), "yyyyMMddhhmmss")
38                     + "_凭证信息" + ".xls";
39             /**
40              * 本次添加主要修复不同浏览器下导出凭证文件名乱码的问题;author ltao
41              */
42             
43             final String userAgent = request.getHeader("USER-AGENT"); 
44             String finalFileName = null;  
45             
46             if (StringUtils.contains(userAgent, "Edge")) {
47                 finalFileName = URLEncoder.encode(strFileName, "UTF8");
48             } else if (StringUtils.contains(userAgent, "MSIE")) {// IE浏览器
49                 finalFileName = URLEncoder.encode(strFileName, "UTF8");
50             } else if (StringUtils.contains(userAgent, "Mozilla")) {// google,火狐浏览器
51                 finalFileName = new String(strFileName.getBytes(), "ISO8859-1");
52             } else {
53                 finalFileName = URLEncoder.encode(strFileName, "UTF8");// 其他浏览器
54             }
55             
56             response.setContentType("application/vnd.ms-excel");
57             response.setHeader("Content-Disposition",
58                     "attachment;fileName=" + finalFileName);
59             ServletOutputStream out = response.getOutputStream();
60             workbook.write(out);
61             out.flush();
62             out.close();
63         } catch (Exception e) {
64             LOG.e(e.toString());
65         }
66         return json;
67     }

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM