Having issues reading excle with some empty rows

P

prashantmantri

I am trying to read excel file using POI with code which looks as
follows

HSSFWorkbook wb = new HSSFWorkbook(stream, false);
HSSFSheet sheet = wb.getSheetAt(sheetNo);
int noOfRows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < noOfRows; r++) {

HSSFRow row = sheet.getRow(r);
if(row == null) break;
//read data

}


What happens is if say I have an excel with 10 rows then i select the
data in last 5 row and delete it and save the excel. Now if I read the
excle again I assume that
int noOfRows = sheet.getPhysicalNumberOfRows(); should give me 5. But
still give me 10 and hence my program fails.

Anyone knows how to handle it?
 
O

Oliver Wong

I am trying to read excel file using POI with code which looks as
follows

HSSFWorkbook wb = new HSSFWorkbook(stream, false);
HSSFSheet sheet = wb.getSheetAt(sheetNo);
int noOfRows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < noOfRows; r++) {

HSSFRow row = sheet.getRow(r);
if(row == null) break;
//read data

}


What happens is if say I have an excel with 10 rows then i select the
data in last 5 row and delete it and save the excel. Now if I read the
excle again I assume that
int noOfRows = sheet.getPhysicalNumberOfRows(); should give me 5. But
still give me 10 and hence my program fails.

Anyone knows how to handle it?

Look at the contents of the rows. They probably have empty strings, or
null or something. Come up with a rule to differentiate "fake" rows from
"real" rows. Implement your count manually based on that rule.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top