LemonMan
LemonMan

Reputation: 3143

Parse xlsx file row by row into String[]

def Reader = new CSVReader(new FileReader("/Users/me/myfile.csv"));
while ((row = Reader.readNext()) != null) {   

How can I do the above with the same format for an xlsx file. I've looked into some options like POI, but i'm not sure how to do exactly what I have above. The examples I saw seemed to require additional code for retrieving the value from a row. In my current case, with opencsv, i just get a row which is a String[] which I can then use like this value = row[index]

Upvotes: 1

Views: 1129

Answers (1)

ejoncas
ejoncas

Reputation: 329

Xslx is a binary format so you can't read it as it were plain text and splitting on something.

You will have to use POI or another library to read excel's files. If you use POI the best way to implement your code is as follow:

FileInputStream file = new FileInputStream(new File("/Users/me/myfile.xsls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt("name of sheet");
for( Row row : sheet.iterator()) {
//reading new row
Cell cell = row.getCell(<your index here>)
//then you can read cell with cell.getStringCellValue()
}

Hope it helps

Upvotes: 3

Related Questions