Jsp/Servlet + POI 导出 Excel
测试环境 Servlet/Struts2 + POI 3.7
支持导出 Excel2003/2007格式,导出文件名支持中文,无乱码
/**
* Jsp/Servlet + POI 导出 Excel
*
* @param response HttpServletResponse
* @param wb HSSFWorkbook/XSSFWorkbook
* @param fileName xxx.xls(97-03)/xxx.xlsx(07-10)
* @throws IOException
*/
publicstaticvoid exportExcel(HttpServletResponse response, Workbook wb,
String fileName) throws IOException {
// 如果文件名有中文,必须URL编码
fileName = URLEncoder.encode(fileName, "UTF-8");
response.reset();
// ContentType 可以不设置
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}