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 ?
Asked
Active
Viewed 1.7k times
5
-
1possible duplicate of [Read xlsx file in Java](http://stackoverflow.com/questions/267025/read-xlsx-file-in-java) – dogbane Jan 17 '11 at 10:27
-
@Srinivas have you tried the library that I've mentioned in the answer ? – koders Sep 06 '15 at 22:33
-
The fastest and the easiest way I found to [read XLS, XLSX, ODS, CSV, etc...](https://www.gemboxsoftware.com/spreadsheet-java/articles/java-read-xls-xlsx-ods-csv-html) is with [GemBox.Spreadsheet for Java](https://www.gemboxsoftware.com/spreadsheet-java). – Hazel Patton Dec 17 '19 at 11:40
6 Answers
2
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.

koders
- 5,654
- 1
- 25
- 20
2
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.

Valentin Rocher
- 11,667
- 45
- 59
-
thank you for the reply,yes i want to write a code that should work for both xlsx ans xls.can you please give me sample code – Srinivas Jan 17 '11 at 10:30
-
org.apache.poi.ss is working for both kind of files thank you very much – Srinivas Jan 29 '11 at 08:17
-
1@Srinivas : if your problem is solved, please mark this question as answered (the little green thing just left of my answer) – Valentin Rocher Jan 31 '11 at 08:18
0
use this for xls and xlsx
Workbook wb_xssf; //Declare XSSF WorkBook
Workbook wb_hssf; //Declare HSSF WorkBook
Sheet sheet=null; //sheet can be used as common for XSSF and HSSF WorkBook
if(fileBean.getFileExt().equalsIgnoreCase("xls")){
wb_hssf = new HSSFWorkbook();
sheet = wb_hssf.getSheetAt(0);
}else if (fileBean.getFileExt().equalsIgnoreCase("xlsx")){
wb_xssf = new XSSFWorkbook(fileBean.getFileInput());
sheet = wb_xssf.getSheetAt(0);
}

jj2422
- 372
- 5
- 20
-
No need for all that, just use `WorkbookFactory.createWorkbook(InputStream)` – EpicPandaForce Feb 20 '15 at 09:54
0
You can try this for xlsx files:
Firstly, you need the following jar downloads:
- dom4j-2.1.0.jar
- poi-3.17.jar
- poi-ooxml-3.17.jar
- commons-collections4-4.1.jar
- xmlbeans-2.3.0.jar
Secondly, add the following imports in your workspace:
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Thirdly, begin building your method, for example for read use this:
public void ReadExcelFiles(String pathxlsx,javax.swing.JTable jtable) throws IOException{
//String nameSheet;
File file = new File(pathxlsx);
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
// nameSheet=wb.getSheetName(0);
//XSSFSheet sh = wb.getSheet(nameSheet);
XSSFSheet sh = wb.getSheetAt(0);
System.out.println(sh.getLastRowNum());
System.out.println("Name: "+sh.getSheetName());
Row row = sh.getRow(6);
System.out.println(row.getRowNum());
System.out.println("columna "+row.getCell(1).getStringCellValue());
System.out.println("columna "+row.getCell(2).getStringCellValue());
System.out.println("columna "+row.getCell(3).getStringCellValue());
System.out.println("columna "+row.getCell(4).getStringCellValue());
System.out.println("Val: "+sh.getRow(4).getCell(6).getStringCellValue());
}

Geovani Diaz
- 79
- 1
- 3
0
You can use the following code and change it (depends on your needs):
public void parseXLSX() {
String pathToXLSX = "file.xlsx";
File file = new File(pathToXLSX);
FileInputStream in = null;
try {
in = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(in);
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
XSSFSheet sheet = workbook.getSheetAt(i);
int rowNumber = sheet.getLastRowNum() + 1;
for (int j = 1; j < rowNumber; j++) {
Iterator it = sheet.getRow(j).cellIterator();
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}
}
} catch (IOException ex) {
ex.getMessage();
ex.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
ex.getMessage();
ex.printStackTrace();
}
}
}
}

Annivey
- 1
-
You need to add (if you use maven) this dependency: `
org.apache.poi poi-ooxml 3.17