gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:08:47 24 4
gpt4 key购买 nike

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

最佳答案

使用以下代码更新单元格值。

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

关于java - 无法使用 java 中的 setCellValue api 更新单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41057500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com