// 下载wb String fileNamestr = "文件名.xls"; OutputStream output = null; try { response.setHeader("Content-disposition","attachment; filename=" + new String(fileNamestr.getBytes("gb2312"), "ISO8859-1" )); response.setCharacterEncoding("UTF-8"); output = response.getOutputStream(); // 读取的文件路径 response.setContentType("application/vnd.ms-excel"); wb.write(output); output.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != output) { try { output.close(); } catch (Exception e1) { e1.printStackTrace(); } } }
附:
1.设置cell背景色不起作用
CellStyle errStyle = sheet.getWorkbook().createCellStyle();
errStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置前景填充样式
errStyle.setFillForegroundColor(HSSFColor.RED.index);
errStyle.setFillBackgroundColor(HSSFColor.RED.index);
2.cell中填充的数值设置为文本样式
CellStyle textCellStyle = wb.createCellStyle();
DataFormat format = wb.createDataFormat();
textCellStyle.setDataFormat(format.getFormat("@"));