How to get SpreadsheetGear.IRange.AutoFit () to specify the correct width for each column?

I am using Spreadsheetgear to create an Excel workbook.

My book is very simple:

  • I insert rows into cells
  • The first line is in bold
  • I draw borders around the headers and columns.

At the end of the process, I call myRange.Columns.AutoFit(), then I save the book.

When I open it with Excel, auto-selection is almost good, but it still skips 1.2 units of width in each column.

Is this a mistake in implementation AutoFit()or am I missing something?

+5
source share
3 answers

SpreadsheetGear , 6 . , b/c AutoFit() , " " . , Excel, . :

SpreadsheetGear Excel , , .NET GDI +, , Excel. , , , , , Excel , . , Excel, .

, Excels ; SpreadsheetGear, , 40-50 "a" . Excel 2007 2010, , . SpreadsheetGear .

, " " , . "", , , Excel.

" fudge", :

ws.UsedRange.Columns.AutoFit()
For col As Integer = 0 To ws.UsedRange.ColumnCount - 1
    ws.Cells(1, col).ColumnWidth *= 1.
Next

#

ws.UsedRange.Columns.AutoFit()
for (int col = 0; col < ws.UsedRange.ColumnCount; col++)
    ws.Cells[1, col].ColumnWidth *= 1.15;
+7

, SpreadsheetGear . SpreadsheetGear 2008, AutoFit(). SpreadsheetGear2010 http://www.spreadsheetgear.com/downloads/whatsnew.aspx, , AutoFit.

+2

Based on the answer from @Stephen ... Excel has a column width limit of 255, so I did this:

    private static void AutoFitColumns(SpreadsheetGear.IWorksheet worksheet)
    {
        worksheet.UsedRange.Columns.AutoFit();

        const int ExcelMaximumColumnWidth = 255;
        const double OneHundredFifteenPercent = 1.15;

        for (var i = 0; i < worksheet.UsedRange.ColumnCount; i++)
        {
            var cell = worksheet.Cells[1, i];
            var width = cell.ColumnWidth * OneHundredFifteenPercent;

            if (width > ExcelMaximumColumnWidth)
                width = ExcelMaximumColumnWidth;

            cell.ColumnWidth = width;
        }
    }
+1
source

All Articles