这就是您使用 apache-poi 的方式。该代码适用于xls和xlsx工作簿。
//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(),则基本上是将更改保存到同一个文件中。
| 归档时间: |
|
| 查看次数: |
2312 次 |
| 最近记录: |