在weblogic上使用Apache poi 3.13导出xlsx:文件格式或扩展名无效

早些时候,我使用Apache POI 2.5.1导出使用HSSFWorkbook .xls文件。 更新Apache POI到3.13我使用SXSSFWorkbook导出.xlsx文件,但导出损坏的文件。

MS Excel无法打开文件格式或扩展名无效的错误。

请注意,这个问题我只面对WebLogic服务器,它在JBoss工作正常。

任何人都可以帮助我在这里做错了吗?

码:

  List<JRField> fields = ds.getFields(); SXSSFWorkbook wb = new SXSSFWorkbook(); SXSSFSheet sheet = wb.createSheet("Sheet1"); try { CellStyle cellStyle = wb.createCellStyle(); CellStyle cellStyleColName = wb.createCellStyle(); CellStyle cellStyleTitle = wb.createCellStyle(); Font boldFont = wb.createFont(); boldFont.setFontHeightInPoints((short)16); boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Cell Style for body cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cellStyle.setWrapText(true); // Cell Style for Column Names cellStyleColName.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cellStyleColName.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyleColName.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); // single line border cellStyleColName.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); // single line border // Cell Style for Title cellStyleTitle.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyleTitle.setFont(boldFont); // Creating Title Row Row row1 = sheet.createRow((short) 0); // Creating the Title line Cell cell1 = row1.createCell((short) 0); cell1.setCellValue("Demo Title"); cell1.setCellStyle(cellStyleTitle); // Title Region CellRangeAddress regionTitle = new CellRangeAddress( (short) 0, // From Row (short) 0, // From Col (short) 0, // To Row (short) (this.displayCols.size()-1) // To Col ); sheet.addMergedRegion(regionTitle); // Column Name Row int j =0; Row row2 = sheet.createRow((short) 1); for (ReportColumn col : this.displayCols) { Cell cell2 = row2.createCell((short) j++); cell2.setCellValue(col.getDisplayName()); cell2.setCellStyle(cellStyleColName); } int i =2; while (ds.next()) { Row rows = sheet.createRow((short) 0 + i); int k = 0; for (JRField field : fields) { String fieldAsString = (ds.getFieldValue(field) != null ? ds.getFieldValue(field).toString():null); Cell cell = rows.createCell((short) k++); cell.setCellStyle(cellStyle); cell.setCellValue(fieldAsString); } i++; if (i > RECORD_LIMIT_FROM_POI){ log.info("Row limit from poi reached #1048576 and exported data is truncated."); break; } } wb.write(os); } catch (Exception e) { log.error("error in createXlsFile method", e); } 

失败的尝试:

  1. application/vnd.ms-excel响应头中的mimetypes更新为vnd.openxmlformats-officedocument.spreadsheetml.sheet
  2. 在WebLogic的自定义MIME映射文件中添加了xlsx=vnd.openxmlformats-officedocument.spreadsheetml.sheet

这可能是更重的资源,但你有没有尝试过:

 XSSFWorkbook wb = new SXSSFWorkbook(); XSSFSheet sheet = wb.createSheet("Sheet1"); 

而不是SXSSF。

这里有不同types的讨论: HSSFWorkbook和XSSFWorkbook以及XSSFWorkbook和SXSSFWorkbook的优缺点?