Search code examples
c#excelwindows-forms-designerwindowsformshost

Export to excel autofit rows not working with c#


Currently, I am using

using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

to help me with the export to excel but how can I set it to auto-size the cell rather then the user do not have to resize then they can read all the words in excel

I have used

xlWorkSheet.UsedRange.Rows.AutoFit();
xlWorkSheet.UsedRange.Columns.AutoFit(); 

but they are not helpful enough

Current Problem


Solution

  • You have to define a range and then you can use

    aRange.Columns.AutoFit();
    

    See the Reference

    or you can use

    worksheet.Columns.AutoFit();
    

    or

    worksheet.Rows.AutoFit();