Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 845
rirhs Avatar asked Apr 25 '11 13:04

rirhs


People also ask

How do I count the number of rows in Excel using poi?

I know of the getLastRowNum() function, which returns a number of rows in an Excel file. The only problem is getLastRowNum() returns a number with the count starting from 0. So if an Excel file uses the first 3 rows, getLastRowNum() returns 2. If an Excel file has just 1 row, getLastRowNum() returns 0.

Does Apache POI help to read Excel file?

Apache POI is a well-trusted library among many other open-source libraries to handle such usecases involving excel files. Please note that, in addition, we can read and write MS Word and MS PowerPoint files also using the Apache POI library.

How many records does Xlsx hold?

xlsx (or . xlsb or . xlsm) workbook has 1048576 rows and 16384 columns. Was this reply helpful?


1 Answers

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...

like image 122
Gagravarr Avatar answered Oct 11 '22 14:10

Gagravarr