java xls和xlsx,在java中从xls和xlsx excel文件读写

这篇博客介绍了如何使用Apache POI的WorkbookFactory创建一个通用接口,以读写xls和xlsx文件,无需关心文件的具体格式。通过示例代码展示了如何读取、写入和编辑Excel文件,无论文件是.xls还是.xlsx格式,代码保持一致。
摘要由CSDN通过智能技术生成

I am writing a program which needs to read and write from excel files, irrespective of the format(xls or xlsx).

I am aware of the Apache POI, but it seems it has different classes to handle xls file(HSSF) and xlsx(XSSF) files.

Anyone aware of how I might achieve what I am trying to do here.

(Ideas for using an API other than POI are also welcome).

解决方案

It's very easy, just use the common SpreadSheet interfaces

Your code would look something like:

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();

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值