我已经使用POI读取/写入Excel工作表。当使用POI写入新工作表时,它工作。但是,当我尝试使用POI在现有的Excel工作簿中编写时,它不起作用。我纠正了这个问题?
workBook = getWorkbookSheet(workBookName);
sheet1 = workBook.getSheetAt(1);
sheet2 = workBook.getSheetAt(2);
while(sheetStart<sheet1.getLastRowNum() + 1)
{
HSSFRow rowSheet1 = sheet1.getRow(sheetStart);
HSSFCell cellSheet1 = rowSheet1.getCell(4);
if(cellSheet1.getStringCellValue().trim().equals(valY))
{ cellSheet1.setCellValue("N");
}
else
//do nothing
sheetStart++;
}
fileOutSheet1 = new FileOutputStream(sheet1.getSheetName());
workBook.write(fileOutSheet1);
fileOutSheet1.flush();
fileOutSheet1.close();
答案 0 :(得分:0)
fileOutSheet1 = new FileOutputStream(sheet1.getSheetName());
似乎不正确。你应该给出工作簿的路径而不是sheet1.getSheetName。因为即使单个单元格内容发生变化,您也在编写整个工作簿。
例如它应该像
FileOutputStream fileOut = new FileOutputStream("C:\\MyWorkbook.xlsx");
wb.write(fileOut);
fileOut.close();
详情请访问here