This project has moved. For the latest updates, please go here.

How to set column width in excel?

Apr 17, 2013 at 2:21 PM
Edited Apr 17, 2013 at 3:02 PM
Does anyone know a convenient function how to set the column width of each column in the worksheet? If possible dpi-aware :) I want to set the column with pixels. E.g. 18pixels for column 1
Apr 17, 2013 at 2:45 PM
I have found a formula but not tried it
width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}] / {Maximum Digit Width} * 256) / 256
Apr 17, 2013 at 3:28 PM
Edited Apr 17, 2013 at 3:28 PM
Try the AutoFit()
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
                    workbook.Activate();
                    sheet.Activate();
                    foreach (Excel.Shape shape in sheet.Shapes)
                    {
                                                sheet.Columns.AutoFit();
                                                sheet.Rows.AutoFit();
                    }
}
It will put itself the size that it requires and you don't need to manually do any calculations.