是否可以先读取然后写入同一个电子表格文件?

Utk*_*rsh 2 java apache-poi

即我正在阅读Cell(0)Row(0)价值,然后我在同一个文件的同一个地方写新的文本。

有没有可能做同样的事情?

Mus*_*afa 5

这就是您使用 apache-poi 的方式。该代码适用于xlsxlsx工作簿。

//Load the workbook into the memory
FileInputStream fis = new FileInputStream("filename_of_your_workbook");
Workbook workbook = WorkbookFactory.create(fis);

//Modify the workbook as you wish
//As an example, we override the first cell of the first row in the first sheet (0-based indices)
workbook.getSheetAt(0).getRow(0).getCell(0).setCellValue("new value for A1");


//you have to close the input stream FIRST before writing to the same file.
fis.close() ;

//save your changes to the same file.
workbook.write(new FileOutputStream("filename_of_your_workbook")); 
workbook.close();
Run Code Online (Sandbox Code Playgroud)

请注意,当您使用 apache-poi 从现有文档创建工作簿对象时,它会将文档模型加载到内存中,并且所有更改都将是临时的,除非您将其保存回文件系统。如果在调用 时提供相同的文件名workbook.write(),则基本上是将更改保存到同一个文件中。