无法使用java中的setCellValue api更新单元格值

时间:2016-12-09 09:49:45

标签: java excel

我想使用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();
    }

1 个答案:

答案 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