Excel 格式 - .xls
我正在尝试将值从一个 Excel 工作表复制/粘贴到另一个现有的 Excel 工作表。我无法在空行中写入值。如果 Excel 行有一些值,那么它正在更新。请检查下面的代码。
我的代码:
FileInputStream file = new FileInputStream(new File(strWorkBookName));
HSSFWorkbook strWorkBook = new HSSFWorkbook(file);
HSSFSheet sheet = strWorkBook.getSheet(sheetName);
// Retrieve the row and check for null
HSSFRow sheetrow = sheet.getRow(rowNo);
if(sheetrow == null){
logger.info("CREATING ROW"+rowNo);
sheetrow = sheet.createRow(rowNo);
}
// Update the value of a cell
HSSFCell cell = sheetrow.getCell(columnNo);
if(cell == null){
logger.info("CREATING COLUMN " + columnNo);
cell = sheetrow.createCell(columnNo); }
cell.setCellValue(valueToUpdate);
FileOutputStream outFile = new FileOutputStream(new File(strWorkBookName));
strWorkBook.write(outFile);
outFile.close();