使用Apache POI在java中读取和写入xls和xlsx excel文件 [英] Reading and writing from xls and xlsx excel file in java using Apache POI

查看:48
本文介绍了使用Apache POI在java中读取和写入xls和xlsx excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,无论格式(xls 或 xlsx)如何,都需要从 excel 文件中读取和写入.

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

我知道 Apache POI,但它似乎有不同的类来处理 xls 文件(HSSF)和 xlsx(XSSF)文件.

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

任何人都知道我可以如何实现我在这里尝试做的事情.(也欢迎使用 POI 以外的 API 的想法).

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).

推荐答案

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

您的代码如下所示:

 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

这篇关于使用Apache POI在java中读取和写入xls和xlsx excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆