tags:

views:

765

answers:

3

hi. i m working an excel object by c#. i want to autofit colunms. but like this: i want that columns width is 5 bigger than autofit mehod set. but how can i do this. how can i know width that autofit method set.

+1  A: 

Assuming that you are on cell A1 & have long text in it, following code will make the column Autofit and then increase the width by 5 characters.

Selection.Columns.Autofit
Selection.Columns(1).ColumnWidth = Selection.Columns(1).ColumnWidth + 5
shahkalpesh
A: 

i tried to use selection.

ExcelApp.Selection

but excelApp.Selection.Colomns is not appeared. how can i use selection correctly.

one of the other problem is that when i autofit rows , some of them is set to big. what problem can cause this?

+1  A: 

If you wish to use Selection and have IntelliSense and early binding, you need to cast the Selection object to a Range first:

  Excel.Range selectedRange = (Excel.Range)myExcelApp.Selection;

  selectedRange.Columns.AutoFit();

  foreach (Excel.Range column in selectedRange.Columns)
  {
      column.ColumnWidth = (double)column.ColumnWidth + 5;
  }

-- Mike

Mike Rosenblum