我想使用java excel API更新特定单元格值,我使用的是下面的代码段,但单元格值没有更新。
public void update_excel() throws IOException
{
FileInputStream excel_file = new FileInputStream(Test.excelfilepath);
XSSFWorkbook workbook = new XSSFWorkbook(excel_file);
XSSFSheet excel_sheet = workbook.getSheetAt(1);
XSSFCell cell1=excel_sheet.createRow(1).createCell(2);
cell1.setCellValue("abc");
excel_file.close();
}
答案 0 :(得分:0)
使用以下代码更新单元格值。
FileInputStream excel_file= new FileInputStream(new File("Test.excelfilepath)); //Read the spreadsheet that needs to be updated
HSSFWorkbook wb = new HSSFWorkbook(excel_file); //Access the workbook
HSSFSheet worksheet = wb.getSheetAt(1); //Access the worksheet, so that we can update / modify it.
Cell cell = null; // declare a Cell object
cell = worksheet.getRow(1).getCell(2); // Access the second cell in second row to update the value
cell.setCellValue("OverRide Value"); // Get current cell value value and overwrite the value
excel_file.close(); //Close the InputStream
FileOutputStream output_file =new FileOutputStream(new File("Test.excelfilepath)); //Open FileOutputStream to write updates
wb.write(output_file); //write changes
output_file.close(); //close the stream