在java中从xls和xlsx excel文件读写 [英] Reading and writing from xls and xlsx excel file in java

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

问题描述

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



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



任何人都知道我可以如何实现我想要做的这里。
(也欢迎使用POI以外的API的想法)。

解决方案

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



您的代码看起来像:

 工作簿wb = WorkbookFactory.create(new File(myFile.xls)); //或.xlsx 
工作表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,只要你使用通用接口


,代码相同

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

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

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