poi不能将数据写入.xlsx

问题是当我试图写入数据到单元格中时,单元格还没有被创build,或者只是没有显示数据。

例如,

Row[] rownames = new Row[names.size()]; for(int i = 0; i < names.size(); i++){ rownames[i] = sheet.createRow(i+3); Cell machine = rownames[i].createCell(0); machine.setCellType(Cell.CELL_TYPE_STRING); machine.setCellValue(names.get(i).toString()); } 

names[]是一个包含名称列表的数组。

Cell machine = rownames[i].createCell(0); 在(i + 3,0)处创build一个单元格,其中i表示行。

machine.setCellValue(names.get(i).toString()); 将单元格值设置为相应的名称[i]。

我尝试打印names[]machine.getStringCellValue() ,他们都可以返回确切的正确数据(如输出到控制台)。 但在xlsx文件中没有任何内容。 提前谢谢了。

编辑:让我更清楚地解释,所以如果一切顺利,这个xlsx文件的部分应该是这样的:

harry | (row 3, col 0) kate | (row 4, col 0) jim | (row 5, col 0) aaron | (row 6, col 0) ... ...

但现在情况是:

| (row 3, col 0) | (row 4, col 0) | (row 5, col 0) | (row 6, col 0) ... ... | (row 3, col 0) | (row 4, col 0) | (row 5, col 0) | (row 6, col 0) ... ...现在xlsx是4KB。 它包含了一些其他的信息,通过这个程序已经放在那里。 那些部分没有这个问题。

看起来像在相同的索引中多次进一步(非发布)代码创build行。

poi如何创buildXSSFRow

 public XSSFRow createRow(int rownum) { CTRow ctRow; XSSFRow prev = _rows.get(rownum); if(prev != null){ // the Cells in an existing row are invalidated on-purpose, in order to clean up correctly, we // need to call the remove, so things like ArrayFormulas and CalculationChain updates are done // correctly. // We remove the cell this way as the internal cell-list is changed by the remove call and // thus would cause ConcurrentModificationException otherwise while(prev.getFirstCellNum() != -1) { prev.removeCell(prev.getCell(prev.getFirstCellNum())); } ctRow = prev.getCTRow(); ctRow.set(CTRow.Factory.newInstance()); } ... } 

所以,如果行存在并包含单元格,则所有包含数据的单元格都将被删除。

为了避免这种情况,使用CellUtil类:

  • 从电子表格中获取一行,如果不存在,则创build它。

     CellUtil.getRow(rowIndex, sheet); 
  • 从一行中获取特定的单元格。 如果单元格不存在,则创build它。

     CellUtil.getCell(row, columnIndex); 

您尚未发布文件开放和结束代码。 根据描述,似乎你没有把数据写回Excel文件。 做这样的事情:

 FileOutputStream out = new FileOutputStream(new File("path of excel file")); wb.write(out); wb.close(); out.close(); 

执行完整的代码后,检查excel文件中是否生成了输出。

 import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 

这是我的代码…

  HSSFFont boldFont; HSSFFont bodyFont; HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("namefile"); HSSFRow row = sheet.createRow((short)0); //HSSFCellStyle cellStyle = workbook.createCellStyle(); //cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index); //cellStyle.setFillForegroundColor(HSSFColor.LAVENDER.index); //cellStyle.setFillPattern(HSSFCellStyle.ALIGN_CENTER); row.setHeightInPoints(23); sheet.setColumnWidth((short)0, (short)2000); sheet.setColumnWidth((short)1, (short)3000); sheet.setColumnWidth((short)2, (short)3000); sheet.setColumnWidth((short)3, (short)3000); sheet.setColumnWidth((short)4, (short)3000); sheet.setColumnWidth((short)5, (short)3000); sheet.setColumnWidth((short)6, (short)3000); sheet.setColumnWidth((short)7, (short)3000); sheet.setColumnWidth((short)8, (short)3000); sheet.setColumnWidth((short)9, (short)3000); sheet.setColumnWidth((short)10, (short)3000); sheet.setColumnWidth((short)11, (short)3000); row.createCell((short)0).setCellValue("FLT NO"); row.createCell((short)1).setCellValue("LEG"); row.createCell((short)2).setCellValue("DATE"); row.createCell((short)3).setCellValue("AC-REG"); row.createCell((short)4).setCellValue("SCHED DEP"); row.createCell((short)5).setCellValue("OFFBLK"); row.createCell((short)6).setCellValue("AIRBORNE"); row.createCell((short)7).setCellValue("LANDING"); row.createCell((short)8).setCellValue("ONBLK"); row.createCell((short)9).setCellValue("SCHED ARR"); row.createCell((short)10).setCellValue("FUEL_USED_PILOT"); row.createCell((short)11).setCellValue("ACTUAL FUEL"); ProofSheetFPRModel model = new ProofSheetFPRModel(); int rownum = 1; for (int i=0; i<DataList.size(); i++) { model = DataList.get(i); row = sheet.createRow((short)rownum); row.createCell((short)0).setCellValue(model.getFlightNo()); row.createCell((short)1).setCellValue(model.getLEG()); row.createCell((short)2).setCellValue(model.getDate()); row.createCell((short)3).setCellValue(model.getAC_REG()); row.createCell((short)4).setCellValue(model.getSCHED_DEP()); row.createCell((short)5).setCellValue(model.getOFF_BLK()); row.createCell((short)6).setCellValue(model.getAIRBORNE()); row.createCell((short)7).setCellValue(model.getLANDG()); row.createCell((short)8).setCellValue(model.getON_BLK()); row.createCell((short)9).setCellValue(model.getSCHED_ARR()); row.createCell((short)10).setCellValue(model.getFUEL_USED_PILOT()); row.createCell((short)11).setCellValue(model.getACTUAL_FUEL()); rownum++; } try { FileOutputStream out = new FileOutputStream(new File(filePath)); workbook.write(out); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }