How to set column width of excel sheet

Rasmita Dash picture Rasmita Dash · Jan 5, 2015 · Viewed 11.5k times · Source

I am using ExcelLibrary for creating excel sheet. I need to increase width of some columns. Is there any way to achieve this?

var ObjWorkBook = new Workbook();
var ObjWorkSheet = new Worksheet(ObjDataTable.TableName);
ObjWorksheet.Cells[0, 0] = new Cell("FirstName");
ObjWorksheet.Cells[0, 1] = new Cell("Email");
//var Width = ObjWorksheet.Cells.ColumnWidth.Default + 200;
ObjWorkBook.Worksheets.Add(ObjWorkSheet);

Answer

Jason Faulkner picture Jason Faulkner · Jan 5, 2015

To change the width of a column, use Range.ColumnWidth.

For example:

// Increase column A by 200
ObjWorksheet.Columns["A"].ColumnWidth += 200

// Alternately.
OjbWorksheet.Columns[1].ColumnWidth += 200

Of course you can put this in a loop to edit multiple columns.

// Increase columns A-E by 200.
for (var i = 1; i <= 5; i += 1)
{
    OjbWorksheet.Columns[i].ColumnWidth += 200
}