Indy Guy
Indy Guy

Reputation: 51

How to change DefaultFontSize for a gembox spreadsheet?

Can anyone give me an example on how to change the font size for a sheet using Gembox software? I was able to change for a cell though but I want to change in entire sheet.

GemBoxHelp

Upvotes: 0

Views: 1442

Answers (1)

GemBox Dev Team
GemBox Dev Team

Reputation: 669

UPDATE 2020-03-27

In latest versions of GemBox.Spreadsheet there are some additional API that simplify this task.

For instance, to set a default font size for the whole Excel file, you can use the following:

var file = ExcelFile.Load("In.xlsx");
file.Styles.Normal.Font.Size = 18 * 20;
file.Save("Out.xlsx");

Or if you want to explicitly specify the font size on each cell in the sheet, you can use the following:

var file = ExcelFile.Load("In.xlsx");
var sheet = file.Worksheets[0];
sheet.Cells.Style.Font.Size = 18 * 20;
file.Save("Out.xlsx");

ORIGINAL

If you have cells that do not have any font related settings (like color, name, size, etc.) applied to them directly then you can just change the cell style's font size, for example:

var file = ExcelFile.Load("In.xlsx");

int size = (int)LengthUnitConverter.Convert(18, LengthUnit.Point, LengthUnit.Twip);
file.Styles[BuiltInCellStyleName.Normal].Font.Size = size;

file.Save("Out.xlsx");

But in a case you do have some directly applied font settings then you will need to iterate through all the allocated cells and apply new size on them:

var file = ExcelFile.Load("In.xlsx");

var sheet = file.Worksheets.ActiveWorksheet;
int size = (int)LengthUnitConverter.Convert(18, LengthUnit.Point, LengthUnit.Twip);

foreach (var row in sheet.Rows)
    foreach (var cell in row.AllocatedCells)
        cell.Style.Font.Size = size;

file.Save("Out.xlsx");

The above refers to the current latest version 3.7, but in the next version 3.9, which we are currently working on, this task is simplified like the following:

sheet.Cells.Style.Font.Size = size;

Upvotes: 1

Related Questions