且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Apache POI在java中读取和写入xls和xlsx excel文件

更新时间:2022-01-11 22:50:18

很简单,使用常见的电子表格界面

您的代码如下所示:

 Workbook wb = WorkbookFactory.create(new File("myFile.xls")); // Or .xlsx
 Sheet s = wb.getSheet(0);
 Row r1 = s.getRow(0);
 r1.createCell(4).setCellValue(4.5);
 r1.createCell(5).setCellValue("Hello");

 FileOutputStream out = new FileOutputStream("newFile.xls"); // Or .xlsx
 wb.write(out);
 out.close();

只要使用通用接口,您就可以使用完全相同的代码读取、写入、编辑现有文件(.xls 和 .xlsx)

You can read, write, edit etc an existing file, both .xls and .xlsx, with exactly the same code as long as you use the common interfaces