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