I need to get the default column width from a XSSFSheet
.
XSSFSheet
has a method called getDefaultColumnWidth()
:
public int getDefaultColumnWidth() {
CTSheetFormatPr pr = worksheet.getSheetFormatPr();
return pr == null ? 8 : (int)pr.getBaseColWidth();
}
The method returns every time the same value, even if I change the default column width within Excel.
Obviously this method should return the default column width but I think it actually doesn't return the correct value.
I checked with Excel itself what exactly happens:
defaultColWidth
got changed.baseColWidth
stayed the same. So the 'real' and correct default column width is stored within the defaultColWidth
attribute. Therefore the method should return that attribute but instead it returns the base column width (as you can see in the implementation above).
In my humble opinion, the method from above should return pr.getDefaultColWidth()
and not pr.getBaseColWidth()
.
I think either the method is wrongly implemented or I'm just too stupid to find the correct method which returns the correct value.
How can I get the correct default column width from an Excel XSSFSheet
with POI Apache?
Regards winklerrr
getDefaultColumnWidth
public int getDefaultColumnWidth()
Get the default column width for the sheet (if the columns do not define their own width) in characters.
Note, this value is different from getColumnWidth(int). The latter is always greater and includes 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines.
Specified by:
getDefaultColumnWidth in interface Sheet
Returns:
column width, default value is 8
The above clearly says that default shall be 8 if columns do not define their own. May be you are using the wrong API.
This problem remains unsolved even in apache poi-5.0.0
.
If you try to get the defaultColWidth
, it returns the baseColWidth
which isn't expected.
So the workaround to get the defaultColWidth
could be accessing the CTWorksheet
to get the value.
private double getDefaultColumnWidth(XSSFSheet sheet) {
if (sheet.getCTWorksheet().isSetSheetFormatPr()) {
CTSheetFormatPr pr = sheet.getCTWorksheet().getSheetFormatPr();
if (pr.isSetDefaultColWidth())
// returns the actual default column width if it has been set
return pr.getDefaultColWidth();
}
// tries to return the baseColWidth and in its absence returns 8
return sheet.getDefaultColumnWidth();
}
There is a clear difference in the way MSExcel
sets the defaultColWidth and the way apache poi-5.0.0
handles it.
MSExcel
sets the default column width as defaultColWidth
.
<sheetFormatPr defaultColWidth="2.5" defaultRowHeight="18.75"/>
This XML
fragment was taken from the document generated by apache poi-5.0.0
. apache poi-5.0.0
sets the default column width as baseColWidth
.
<sheetFormatPr baseColWidth="2.5" defaultRowHeight="18.75" customHeight="true"/>
I know it has been almost six years since this question had been last posted but it may be helpful to someone else I guess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.