下面是代码.所有代码都能执行完,没有报错,也不弹出保存窗口!求助...
/**
* 导出
*/
public void exportData(HttpServletResponse response) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = null;
// 创建一行
HSSFRow row = null;
HSSFCell cell = null;
HSSFCellStyle stringstyle = null;
HSSFCellStyle headStyle = null;
headStyle = workbook.createCellStyle();
headStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
headStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
headStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
headStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
headStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
headStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
headStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
HSSFFont headfont = workbook.createFont();
// 设置头部字体为宋体
headfont.setFontName("宋体");
// 粗体
headfont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headfont.setFontHeightInPoints((short) 11);
// 单元格样式使用字体
headStyle.setFont(headfont);
HSSFFont datafont = workbook.createFont();
stringstyle = workbook.createCellStyle();
stringstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
stringstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
stringstyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
stringstyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
stringstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 单元格样式使用字体
stringstyle.setFont(datafont);
sheet = (HSSFSheet) workbook.createSheet();
sheet.setDefaultColumnWidth(16);
//合并起始行,起始列,结束行,结束列
CellRangeAddress title1 = new CellRangeAddress(0, 0, 0, 10);
sheet.addMergedRegion(title1);
//设置第一行
row = (HSSFRow) sheet.createRow(0);
cell = (HSSFCell) row.createCell(0);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("部室员工绩效合约考核汇总表");
cell.setCellStyle(headStyle);
//合并起始行,起始列,结束行,结束列
// CellRangeAddress row1 = new CellRangeAddress(1, 0, 1, 1);
// sheet.addMergedRegion(row1);
//设置第二行
row = (HSSFRow) sheet.createRow(1);
cell = (HSSFCell) row.createCell(0);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("项目");
cell.setCellStyle(headStyle);
cell = (HSSFCell) row.createCell(1);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("员工1");
cell.setCellStyle(headStyle);
cell = (HSSFCell) row.createCell(2);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("员工2");
cell.setCellStyle(headStyle);
//合并起始行,起始列,结束行,结束列
// CellRangeAddress row2 = new CellRangeAddress(2, 0, 2, 1);
// sheet.addMergedRegion(row2);
//设置第三行
row = (HSSFRow) sheet.createRow(2);
cell = (HSSFCell) row.createCell(0);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("一、主体指标");
cell.setCellStyle(headStyle);
cell = (HSSFCell) row.createCell(1);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("111");
cell.setCellStyle(headStyle);
cell = (HSSFCell) row.createCell(1);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue("222");
cell.setCellStyle(headStyle);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(("季度绩效工资分配.xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}