在java中从xls和xlsx excel文件读写 [英] Reading and writing from xls and xlsx excel file in java
问题描述
我知道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屋!