Excel AutoSizeColumn

Aug 5, 2009 at 7:00 AM

Hello,

This method 'AutoSizeColumn(ColumnIndex)'  doesn't work...

It resizes the columns but not properly.

NPOI Version 1.2.1.

[Code]

for (int i = this.SheetNumber; i >= 0; i--)
            {
                int indexCell = 0;
                HSSFCell headerCellToAutoSize = this._ExcelDocument.GetSheetAt(i).GetRow(0).GetCell(indexCell);
                while (headerCellToAutoSize != null)
                {
                    this._ExcelDocument.GetSheetAt(i).AutoSizeColumn(headerCellToAutoSize.ColumnIndex);
                    indexCell++;
                    headerCellToAutoSize = this._ExcelDocument.GetSheetAt(i).GetRow(0).GetCell(indexCell);
                }
            }

[/Code]
François.

 

Coordinator
Aug 11, 2009 at 1:21 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jun 1, 2010 at 1:50 PM
Edited Jun 2, 2010 at 11:44 AM

I've found that the following doesn't work, at least not consistently, with v1.2.2. The 1st 2-columns are getting set, but not the last one. At the point where this snippet is executed, the entire worksheet has been populated, and the cell style and font for row #0, containing the column titles, has been set. When I open the the file with Excel and manually set the column to AutoFit, it adds 15-pixels to the width of the column; from 112-pixels to 127.

      IEnumerator columns = ws.GetRow(0).GetCellEnumerator();
      while (columns.MoveNext())
      {
        HSSFCell column = (HSSFCell)columns.Current;
        ws.AutoSizeColumn(column.ColumnIndex);
      }