无法调用 "org.apache.poi.ss.usermodel.Cell.setCellValue(String)",因为 "cell" 对于现有 XLSM 文件为空
Cannot invoke "org.apache.poi.ss.usermodel.Cell.setCellValue(String)" because "cell" is null for existing XLSM file
我使用 Apache POI 将数据写入预定义的 XLSM 文件。我使用此代码打开现有文件:
Cell cell;
File file = new File(XLSMPath);
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(recordcount+4);
数据在第 5 行的第一次迭代中写入,依此类推。设置给定单元格值的代码:
cell = row.getCell(CellReference.convertColStringToIndex("A"));
cell.setCellValue(myvalue);
它在前 400 次迭代中运行良好,但之后我收到以下错误消息:
Cannot invoke "org.apache.poi.ss.usermodel.Cell.setCellValue(String)"
because "cell" is null
您需要自己创建工作表单元格。只需检查您的 Cell
是否为 null
并使用 createCell(int)
:
创建新的 Cell
cell = row.getCell(CellReference.convertColStringToIndex("A"));
if (cell == null) {
// maybe in your case index should be taken in other way
cell = row.createCell(CellReference.convertColStringToIndex("A"));
}
cell.setCellValue(myvalue);
我使用 Apache POI 将数据写入预定义的 XLSM 文件。我使用此代码打开现有文件:
Cell cell;
File file = new File(XLSMPath);
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(recordcount+4);
数据在第 5 行的第一次迭代中写入,依此类推。设置给定单元格值的代码:
cell = row.getCell(CellReference.convertColStringToIndex("A"));
cell.setCellValue(myvalue);
它在前 400 次迭代中运行良好,但之后我收到以下错误消息:
Cannot invoke "org.apache.poi.ss.usermodel.Cell.setCellValue(String)" because "cell" is null
您需要自己创建工作表单元格。只需检查您的 Cell
是否为 null
并使用 createCell(int)
:
Cell
cell = row.getCell(CellReference.convertColStringToIndex("A"));
if (cell == null) {
// maybe in your case index should be taken in other way
cell = row.createCell(CellReference.convertColStringToIndex("A"));
}
cell.setCellValue(myvalue);