Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read .xlsx and .xls files in Java?

Tags:

java

xlsx

xls

xssf

hi i want to read xlsx file or xls file what ever it is. can XSSF support xls file ? or do i need to write the separate code for both kind of files ?

like image 351
Srinivas Avatar asked Jan 17 '11 10:01

Srinivas


3 Answers

Yes, you can use Apache POI to read and write xlsx and xls files.

like image 198
Murukesh Avatar answered Oct 04 '22 21:10

Murukesh


If you want your code to work for both, you'll have to use the org.apache.poi.ss package. This package has been created to unify XSSF and HSSF.

like image 34
Valentin Rocher Avatar answered Oct 04 '22 20:10

Valentin Rocher


For one of my projects I have created a basic utility that uses Apache POI and OpenCSV and can read both xlsx, xls and csv files.

Given a converter it can convert rows to objects, like this:

RowConverter<Country> converter = (row) -> new Country(row[0], row[1]);

ExcelReader<Country> reader = ExcelReader.builder(Country.class)
     .converter(converter)
     .withHeader()
     .csvDelimiter(';')
     .sheets(1)
     .build();

List<Country> list;
list = reader.read("CountryCodes.xlsx");
list = reader.read("CountryCodes.xls");
list = reader.read("CountryCodes.csv");

You may find the project on github.

like image 43
eaorak Avatar answered Oct 04 '22 22:10

eaorak