Настройки шрифта

Настройка настроек шрифта

Aspose.Cells предоставляет класс Workbook, который представляет файл Microsoft Excel. Класс Workbook содержит коллекцию Worksheets, которая позволяет получить доступ к каждому листу в файле Excel. Лист представлен классом Worksheet. Класс Worksheet предоставляет коллекцию Cells. Каждый элемент в коллекции Cells представляет объект класса Cell.

Aspose.Cells предоставляет методы класса Cell для получения и установки стиля форматирования ячейки. Класс GetStyle предоставляет свойства для настройки настроек шрифта.

Установка названия шрифта

Разработчики могут применить любой шрифт к тексту внутри ячейки, используя свойство Name объекта Style.Font.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font name to "Times New Roman"
style.Font.Name = "Times New Roman";
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Установка стиля шрифта на жирный

Разработчики могут сделать текст жирным, установив свойство IsBold объекта Style.Font в true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font weight to bold
style.Font.IsBold = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Установка размера шрифта

Установите размер шрифта с помощью свойства Size объекта Style.Font.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font size to 14
style.Font.Size = 14;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Установка цвета шрифта

Используйте свойство Color объекта Style.Font для установки цвета шрифта. Выберите любой цвет из перечисления Color (часть .NET framework) и присвойте его свойству Color.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font color to blue
style.Font.Color = Color.Blue;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Установка типа подчеркивания шрифта

Используйте свойство Underline объекта Style.Font для подчеркивания текста. Aspose.Cells предлагает различные предопределенные типы шрифтов в перечислении FontUnderlineType.

Типы подчеркивания шрифта Описание
Accounting Одиночная линия подчеркивания для учета
Double Двойное подчеркивание
DoubleAccounting Двойная линия подчеркивания для учета
None Без подчеркивания
Single Одиночная линия подчеркивания
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font to be underlined
style.Font.Underline = FontUnderlineType.Single;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Установка эффекта зачеркивания

Примените зачеркивание, установив свойство IsStrikeout объекта Style.Font в true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the strike out effect on the font
style.Font.IsStrikeout = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Установка эффекта нижнего индекса

Примените нижний индекс, установив свойство IsSubScript объекта Style.Font в true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting subscript effect
style.Font.IsSubscript = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Установка верхнего индекса на шрифт

Разработчики могут применить эффект верхнего индекса к шрифту, установив свойство IsSuperscript объекта Style.Font в true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting superscript effect
style.Font.IsSuperscript = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Продвинутые темы