简体   繁体   中英

How to get the default column width of an Excel sheet with Apache-POI?

Requirement

I need to get the default column width from a XSSFSheet .

Approach

XSSFSheet has a method called getDefaultColumnWidth() :

public int getDefaultColumnWidth() {
    CTSheetFormatPr pr = worksheet.getSheetFormatPr();
    return pr == null ? 8 : (int)pr.getBaseColWidth();
}

Problem

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.

Cause

I checked with Excel itself what exactly happens:

  1. I changed the default column width within Excel and saved the sheet.
  2. I then look up the changes Excel made in the corresponding xml file:
    • The attribute defaultColWidth got changed.
    • The attribute 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).

Solution

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.

Question

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM