How to get the max no. of columns filled in an XLSX file using POI?

12,044

There's a dimension object on XSSF Sheets too. Try:

CTSheetDimension dimension = sheet.getCTWorksheet().getDimension();
String sheetDimensions = dimenson.getRef();

The one issue that springs to mind is I'm not sure if it's required for the dimension (CTDimensions or DimensionsRecord) to always be correct...

Share:
12,044
rirhs
Author by

rirhs

Updated on June 21, 2022

Comments

  • rirhs
    rirhs about 2 years

    I know we can get the max number of columns by iterating over all the rows and calling getLastCellNumber on each row object.. but this approach requires iterating over all the rows which I want to avoid since it will take lot of time for files with a million rows(that’s the kind of files I am expecting to be read).

    When POI reads a excel file, it stores the sheet dimensions (first row number, last row number , first col number, last col number) in an object of the DimensionsRecord class. So if I get this object I will get what I need. These objects can be obtained from the Sheet class which is an inner class of POI. I was able to extract what I need for XLS files, but I have hit a roadblock for XLSX files.

    Does POI maintain DimensionsRecord object for XLSX also?, if yes has anybody tried to extract it? Or Is there some other by which this can be done?? please help!

    Also I wanted to ask, whether my approach is correct or not, i.e I am using the inner classes of POI (it is getting my work done), is this correct or should I solely rely on exposed APIs (too time consuming).

  • rirhs
    rirhs about 13 years
    thanx @Gagravarr for the reply.. I tried it and it is working fine..though I dont understand why CTDimensions can be wrong.. can you please tell how harmful can it be to use this ... also for extracting DimensionsRecord I have a very long procedure ( about 30 lines of code ) I am doing it by creating POI's internal sheet objects and then getting DimensionsRecord from the records list... is there a short way of doing it like the way you have done with XLSX CTDimensions??
  • Gagravarr
    Gagravarr about 13 years
    Excel certainly ought to keep the records up to date, but I'm not sure if other programs will do. See line 387 of hssf.model.InternalSheet for all the comments about it not always being there / being updated. To get a DimensionsRecord, you'll want to grab the records off the InternalSheet.
  • rirhs
    rirhs about 13 years
    I checked the source code of internal sheet (poi lib 3.7 ) , check this link grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/poi/‌​3.7/… here above line 531 a comments states that the dimension record is set correctly.
  • Gagravarr
    Gagravarr about 13 years
    POI sets it, but it's other programs (Crystal Reports, Perl etc) that may well not