AutoFit Column in Exce in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to auto fit column in Excel workbook via Spire.XLS.

using Spire.Xls;

namespace AutofitColumn
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\..\..\Data\country.xls");
            Worksheet sheet = workbook.Worksheets[0];

            //autofit the third column
            sheet.AutoFitColumn(1);

            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start("sample.xls");
        }
    }
}

Imports Spire.Xls

Namespace AutofitColumn
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\..\..\..\Data\country.xls")
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'autofit the third column
			sheet.AutoFitColumn(1)

			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start("sample.xls")
		End Sub
	End Class
End Namespace