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

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

问题描述

我正在编写一个程序,该程序需要从excel文件读取和写入,而不考虑格式(xls或xlsx).

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

推荐答案

非常简单,只需使用常见的 SpreadSheet界面

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

您的代码应类似于:

 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天全站免登陆