Java POI XSSF覆盖excel文件中的特定单元格
我正在尝试写入现有的excel文件。我不想创建新行或单元格,我只想将数组中的值写入x行y列中的值。到目前为止,每次我都尝试了这个方法,只有创建一个新行,我才能让它工作。请帮忙Java POI XSSF覆盖excel文件中的特定单元格,java,excel,apache-poi,Java,Excel,Apache Poi,我正在尝试写入现有的excel文件。我不想创建新行或单元格,我只想将数组中的值写入x行y列中的值。到目前为止,每次我都尝试了这个方法,只有创建一个新行,我才能让它工作。请帮忙 Integer columns = DataImport.columns_in_sheet[0]; Integer rowNum = learnerRow + 2; try { FileInputStream inp = new FileInputStr
Integer columns = DataImport.columns_in_sheet[0];
Integer rowNum = learnerRow + 2;
try {
FileInputStream inp = new FileInputStream("D:/location/update.xlsx");
XSSFWorkbook wb = null;
wb = (XSSFWorkbook) WorkbookFactory.create(inp);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(18);//places the start row
XSSFCell cell = null;//places the start column
cell = row.getCell(0);
//#########################################################################################
//#########################################################################################
for (int j = 0; j < exportData.length; j++) {
//sheet.createRow(rowNum+j);
//row = sheet.getRow(rowNum+j);
//row = sheet.getRow(rowNum+j);
for (int i=0; i < columns;i++){
cell.setCellType(CellType.STRING);
cell.setCellValue(exportData[j][i]);
}
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("D:/location/update.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Integer columns=DataImport.columns_在_工作表[0]中;
整数rowNum=learnerRow+2;
试一试{
FileInputStream inp=newfileinputstream(“D:/location/update.xlsx”);
xssfwb=null;
wb=(XSSF工作簿)WorkbookFactory.create(inp);
XSSFSheet-sheet=wb.getSheetAt(0);
XSSFRow row=sheet.getRow(18);//放置起始行
XSSFCell cell=null;//放置起始列
cell=row.getCell(0);
//#########################################################################################
//#########################################################################################
对于(int j=0;j
这段代码抛出了一个空指针,因为行为空,我似乎只能通过创建新行来消除错误。我正在使用XSSF格式。您的代码片段的逻辑不清楚。我觉得这不合逻辑 但是,为了避免在使用当前工作表中的行和单元格时出现NPE,始终需要检查该行或单元格是否已经存在或需要新建。这是必需的,因为对于不存在的行
Sheet.getRow
将返回null
。另外,对于不存在的单元格,Row.getCell
将返回null
因此,我们可以:
Sheet sheet = ...
Row row = sheet.getRow(rowIdx); if (row == null) row = sheet.createRow(rowIdx);
Cell cell = row.getCell(cellIdx); if (cell == null) cell = row.createCell(cellIdx);
现在,行
要么是已经存在的行,要么是新创建的行。而单元格
要么是已经存在的单元格,要么是新创建的单元格。行
和单元格
都将不为空
。首先,如果不存在行/单元格,则会在创建新行/单元格之前获取它们。因此,当前的行和单元格不会被破坏
循环中也需要相同的功能:
Sheet sheet = ...
Row row;
Cell cell;
for (int rowIdx = 0; rowIdx < 10; rowIdx++) {
row = sheet.getRow(rowIdx); if (row == null) row = sheet.createRow(rowIdx);
for (int cellIdx = 0; cellIdx < 10; cellIdx++) {
cell = row.getCell(cellIdx); if (cell == null) cell = row.createCell(cellIdx);
// do something with cell
}
}
工作表=。。。
行行;
细胞;
对于(int-rowIdx=0;rowIdx<10;rowIdx++){
row=sheet.getRow(rowIdx);if(row==null)row=sheet.createRow(rowIdx);
对于(int-cellIdx=0;cellIdx<10;cellIdx++){
cell=row.getCell(cellIdx);if(cell==null)cell=row.createCell(cellIdx);
//用手机做点什么
}
}
非常感谢您,我一直在为一件不知道如何在几秒钟内解决的事情绞尽脑汁。修改了我的代码,一切正常,