Skip to main content Skip to footer

How to autofit columns of an Excel table in .NET using GcExcel

Issue:

The AutoFit method of GcExcel works fine for normal columns of Excel, but when using a table and calling the AutoFit method of the Columns class, the last character is overlapped by the dropdown sign.

Resolution:

To make it look proper, you can use the code snippet given below:

Workbook wb = new Workbook();
IWorksheet sheet = wb.Worksheets[0];
sheet.Rows[0].Value = new object[] { "TotalColumnHeader", "TotalColumnHeader" };
ITable table = sheet.Tables.Add(sheet.Range["A1:B2"], true);
sheet.Range["A1:B2"].Columns.AutoFit();
for(int i=0;i<table.Columns.Count;i++)
sheet.Columns[i].ColumnWidth = sheet.Columns[i].ColumnWidth + 2;
wb.Save("demo.xlsx");

Tags:

Prabhat Sharma