Apache poi方法将数据写入现有的工作簿

这是我的class级阅读和写入现有的Excel文件。 我一直通过传递filePath和fileName来调用主类中的这些函数。

public class NewExcelFile { Workbook workbook; /******* Methods *******/ // returns a workbook on giving the excel file's path and name public Workbook readExcel(String filePath, String fileName) { // Create object of File class to open xlsx file File file = new File(filePath + "\\" + fileName); // Create an object of FileInputStream class to read excel file FileInputStream inputStream = null; try { inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { System.out.println("Error: Unable to find " + fileName + " in " + filePath); e.printStackTrace(); } Workbook workbook = null; // Find the file extension by spliting file name in substring and // getting only extension name String fileExtensionName = fileName.substring(fileName.indexOf(".")); // Check condition if the file is xlsx file if (fileExtensionName.equals(".xlsx")) { // If it is xlsx file then create object of XSSFWorkbook class try { workbook = new XSSFWorkbook(inputStream); } catch (IOException e) { e.printStackTrace(); } } // Check condition if the file is xls file else if (fileExtensionName.equals(".xls")) { // If it is xls file then create object of XSSFWorkbook class try { workbook = new HSSFWorkbook(inputStream); } catch (IOException e) { e.printStackTrace(); } } this.workbook = workbook; return workbook; } public void writeExcel(String filePath, String fileName, String sheetName, String dataToWrite, int rowno) { System.out.println("WriteExcel" + filePath + " " + fileName + " " + sheetName + " " + dataToWrite + " " + rowno); Workbook newWorkbook = readExcel(filePath, fileName); Sheet sheet = newWorkbook.getSheet(sheetName); System.out.println("Sheet: " + sheet.getSheetName()); Cell resultcell; ******resultcell = sheet.getRow(rowno).createCell(8); resultcell.setCellType(Cell.CELL_TYPE_STRING); resultcell.setCellValue(dataToWrite); CellStyle style = workbook.createCellStyle(); if (dataToWrite == "P") { style.setFillBackgroundColor(IndexedColors.GREEN.getIndex()); style.setFillPattern(CellStyle.ALIGN_FILL); resultcell.setCellStyle(style); } else if (dataToWrite == "F") { style.setFillBackgroundColor(IndexedColors.RED.getIndex()); style.setFillPattern(CellStyle.ALIGN_FILL); resultcell.setCellStyle(style); } // Create an object of FileOutputStream class to create write data in // excel file File file = new File(filePath + "\\" + fileName); FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(file); } catch (FileNotFoundException e) { System.out.println("File not found"); e.printStackTrace(); } // write data in the excel file and close output stream try { workbook.write(outputStream); outputStream.close(); } catch (IOException e) { System.out.println("Error in writing to file"); e.printStackTrace(); } } 

当我使用readExcel获取主工作簿并调用此函数时:

 Row row = testScriptsSheet.getRow(24); 

我得到了正确的行,并能够调用这一行上的所有函数。但是对于writeExcel()中完全相同的表中的完全相同的行,我得到一个空指针exception(代码中以***开头的行以上)。 getRow()在这里给我null。 我在这里做错了什么?

另外,我是否应该将工作簿保存为数据成员,并在需要时执行myNewExcelFile.workbook ,或者将其保留为readExcel在主类中返回的variables? 另外我想知道现在发生了什么,我没有closuresreadExcel函数末尾的inputStream。 无论是否closuresinputStream,我都会得到同样的错误。

编辑 – 添加主要function

 public class NewDriver { public static void main(String[] args) { System.out.println("Starting the framework"); // initialise the workbook NewExcelFile testExecution = new NewExcelFile(); testExecution.readExcel(System.getProperty("user.dir") + "\\", "abc.xlsx"); // initialise sheets of workbook Sheet testSuiteSheet = testExecution.workbook.getSheet("TestSuite"); Sheet testScriptsSheet = testExecution.workbook.getSheet("TestCases"); Row row = testScriptsSheet.getRow(24);//gives the correct row //calling writeExcel gives npe in that line } } } 

}

从文档 getRow(int)方法:

返回逻辑行(不是物理的)0。 如果你要求一个没有定义的行,你会得到一个空值。 这就是说,第4行代表了表单上的第五行。

所以当一行没有被定义的时候,你必须先创build行然后创build单元格。

根据我的理解,这似乎是一个概念错误。 在调用WriteExcel()方法之前,您在main方法中所做的所有更改都是在缓冲区中,而不是写在硬盘中的Excel表单/工作簿中。 但是,在WriteExcel()方法中,您并不是将保存在缓冲区中的工作表或工作簿传递给硬盘驱动器中的地址。 所以你在主函数中所做的任何更改都不存在,因此显示空指针exception。

例如我有一个工作簿在说我的D驱动器,在A0值为1。 现在我已经编程使它2,但不执行写操作,并搁置执行。 同时,我去了我的D驱动器,并打开表单将有1不是2,因为更新的值在缓冲区,直到我已经执行该工作簿上的写操作。

build议:而不是通过工作簿的地址,为什么不通过你在主要方法中使用的工作簿。

更新:不是主要的方法,但readExcel(String filePath, String fileName)方法。