标签:
以下是原创作品,转载请标明出处http://lvjava.com/?p=269
直接上在Spring中下载Excel文件的例子,重点是包含有utf8的那一句,将ISO-8859-1编码格式改为utf8格式的。
对于IE浏览器必须使用URLEncoder转换,不然下载的时候会报错。
其中fileName = fileName.replaceAll(“\\+”, “%20″);可以修正URLEncoder将空格转换成+号的BUG。
@RequestMapping(method = RequestMethod.GET, params="action=exportExcel") public void envExportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("application/vnd.ms-excel");//设置输出格式 Excel 头信息. String fileName = "测试文件名"; String agent = request.getHeader("USER-AGENT"); if(agent != null && agent.indexOf("MSIE") != -1) { fileName = URLEncoder.encode(fileName, "UTF8"); fileName = fileName.replaceAll("\\+", "%20"); } else { fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); } response.setHeader("Content-Disposition","attachment; filename=\"" + fileName + ".xls" + "\";target=_blank"); OutputStream os = response.getOutputStream(); HSSFWorkbook wb = service.exportExcelFile(); wb.write(os); os.close(); response.flushBuffer(); }
标签:
原文地址:http://www.cnblogs.com/LOHAS/p/4213859.html