We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Formatting range of cells

Is there an easy way to format a range of cells that has different formatting? Ex. cells in a range have different font and all I’m trying to do is to change all cells’ font size.

Hi Vlad,

Thanks for your posting and using Aspose.Cells.

You should first create a range and then apply style using Range.ApplyStyle() method. You can give it your Style object and StyleFlag object. Since, you want to change the font size only, you will set the StyleFlag.FontSize property true. If you want to change everything, you will set StyleFlag.All property to true.

Please see the following code. It just changes the font size of the range “D3:G3” and leave everything unchanged. I have attached the sample Excel file used in this code and the output Excel file generated by it and screenshot for your reference.

The code is fully commented, so you will not find it difficult to understand.

C#


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


//Create workbook object

Workbook workbook = new Workbook(filePath);


//Access first worksheet

Worksheet worksheet = workbook.Worksheets[0];


//Create a range where you want to apply style

Range range = worksheet.Cells.CreateRange(“D3:G3”);


//This is our style

Style style = workbook.CreateStyle();

style.Font.Size = 16;


//This flag tells, what you want to change.

//Since we only want to change font size, so we have made that property true

//If you want to change everything then use StyleFlag.All property and set it true

StyleFlag flag = new StyleFlag();

flag.FontSize = true;


//Apply style to range of cells

range.ApplyStyle(style, flag);


//Save the workbook on disk

workbook.Save(“output.xlsx”);