Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Apache POI column Iternator

 
sai rama krishna
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have method to read XLS as below

I have attached sample XLS.

I need to convert that XLS file to XML file as attached using apache POI and java program.

I need to iterate the XLS file based on first column to do all this. If first column is null ignore. Instead it has values like

AAA9115
AAA9116
AAA911
AAA9119
AAA9118



BBB911
BBB912
BBB913
BBB914
BBB919
BBB9117
BBB9114
BBB9113



then read that row and then to convert it to XML.
each tag _1, _2, _3,_4,_5,_6 gets values from corresponding XLS cells.

I looked for column iterator similar to row iterator to check if it is null or not
//Iterator colIter = mySheet.colIterator();
but API does not have it. How Can I achieve this task.

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
xls1.JPG
[Thumbnail for xls1.JPG]
xls file
xml1.JPG
[Thumbnail for xml1.JPG]
xml file
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need a column iterator? It's easy to get at any individual cell via HSSFSheet.getRow(...).getCell(...).

By the way, I advise to use the classes in the org.apache.poi.ss package instead of the ones in org.apache.poi.hssf. That way, you can process XSLX files in addition to XLS files.
 
sai rama krishna
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure.

How to check on a row and see if the first column of that partical row is not null right

can i modify below line
>> while(rowIter.hasNext()){
to something as shown below(i am not sure exactly how to check)
>> while(rowIter.hasNext()& not null){

please advise
 
sai rama krishna
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking like

>>if (cell.getStringCellValue() != null && !cell.getStringCellValue().lenght==0)


but not sure how to check it for first cell(column) of each row.

Please advise
 
sai rama krishna
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kind of wrote like above that is printing all the values in a row even if the first one is still null .

Instead i
want to get all the values in a row if the first one isn't null

Please advise

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code does seem to check for "if (myRow.getCell(0) != null){" - if that's not the correct implementation of "if the first one isn't null", then how do you define it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic