日期:2014-05-19 浏览次数:20780 次
String fileName = DateTimeUtil.dateTimeToStr(new Date(),
DateTimeUtil.STR_DATETIME_PATTERN);
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ fileName + ".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
HSSFWorkbook wb = new HSSFWorkbook();// 建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("sheet");// 建立新的sheet对象
HSSFRow head = sheet.createRow((short) 0);
head.createCell((short) 0).setCellValue("发送时间");
head.createCell((short) 1).setCellValue("短信名称");
head.createCell((short) 2).setCellValue("发送总数");
head.createCell((short) 3).setCellValue("成功次数 ");
head.createCell((short) 4).setCellValue("失败次数");
head.createCell((short) 5).setCellValue("成功率");
head.createCell((short) 6).setCellValue("");
for (int i = 0; i < list.size(); i++) {
SmsCdr item = list.get(i);
HSSFRow row = sheet.createRow((short) i+1);// 建立新行
row.createCell((short) 0).setCellValue(item.getTempTime());
row.createCell((short) 1).setCellValue(item.getServiceName());
row.createCell((short) 2).setCellValue(item.getTotalCount());
row.createCell((short) 3).setCellValue(item.getSuccessCount());
row.createCell((short) 4).setCellValue(item.getFailCount());
row.createCell((short) 5).setCellValue(item.getSuccessRate());
}
ServletOutputStream fileOut = response.getOutputStream();
fileOut.write(wb.getBytes());
fileOut.close();
response.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");
response.setContentType("application/octet-stream;charset=UTF-8");
workbook.write(response.getOutputStream());