Apply the same width to column in ranges

Hi there,


If I have to apply the same width for columns from D to BD, starting from row 6, how would i do that? Could I do this using ranges?

I have attached a sample file for reference.

Please advise!!

Hi,

You cannot use range, you will have to use a loop.

See the code below, it does as required by you. Please see the output file.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\ColumnRangesWidth.xls”;


Workbook workbook = new Workbook(filePath);

Worksheet worksheet = workbook.Worksheets[0];


//Get column indices of D to BD

int colD = CellsHelper.ColumnNameToIndex(“D”);

int colBD = CellsHelper.ColumnNameToIndex(“BD”);


for (int i = colD; i <= colBD; i++)

{

worksheet.Cells.SetColumnWidth(i, 25);

}



workbook.Save(filePath + “.out.xlsx”);

Thanks a bunch.

Hi,

Here is even more easy way that does not need for loop iterations instead it uses Range.ColumWidth property.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\ColumnRangesWidth.xls”;


Workbook workbook = new Workbook(filePath);


Worksheet worksheet = workbook.Worksheets[0];


Range range = worksheet.Cells.CreateRange(“D1:BD1”);


range.ColumnWidth = 25;


workbook.Save(filePath + “.out.xls”);