C # Auto-fit method for excel sheet

I am creating an excel sheet using C # .net. Eventually I want to auto-adjust the columns. I am using the following code.

    sheet0.Cells["A2:B2"].Columns.AutoFit();

      

But this is an exception. Can anyone help me solve this problem?

Thank.

+3


source to share


2 answers


Try it like this:



sheet0.Columns["A:B"].AutoFit();

      

+11


source


You can use:

sheet0.Columns.AutoFit();

      




This will resize each column in that sheet to best fit.

AutoFit Documentation:

Changes the width of columns in a range or the height of rows in a range to achieve the best fit.

+18


source







All Articles