JAVA写EXCLE写出来的文件为什么在打开的时候报错
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();
一般都是导入CSV格式的,execl里面缩包含的格式太多,容易出现问题。
出现这个问题感觉应该是数据丢失的问题,就是你写的这个程序中好像少输出了一些东西。。
等会有空我也学学转execl的在看吧。
response.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"\""); response.setContentType("application/octet-stream;charset=UTF-8"); workbook.write(response.getOutputStream());