格式化一系列单元格

设置一系列单元格的字体和样式

在我们讨论格式设置之前(我们在之前的主题中已经谈过很多),我们应该了解如何创建一系列单元格。嗯,我们可以使用CellRange类来创建一系列单元格,它的构造函数接受一些参数来指定单元格的范围。我们可以使用Names行列索引来指定起始和结束单元格的范围。

一旦我们创建了CellRange对象,我们就可以使用Worksheet的重载版本的SetStyleSetFontSetFontColor方法,这些方法可以接受一个CellRange对象来应用指定范围的单元格上的格式设置。

让我们通过一个例子来了解这个基本概念。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
Worksheet sheet = gridDesktop1.GetActiveWorksheet();
// Setting sample values
GridCell cell = sheet.Cells["b7"];
cell.SetCellValue("1");
cell = sheet.Cells["c7"];
cell.SetCellValue("2");
cell = sheet.Cells["d7"];
cell.SetCellValue("3");
cell = sheet.Cells["e7"];
cell.SetCellValue("4");
// Creating a CellRange object starting from "B7" to "E7"
CellRange range = new CellRange(6, 1, 6, 4);
// Accessing and setting Style attributes
Style style = new Style(this.gridDesktop1);
style.Color = Color.Yellow;
// Applying Style object on the range of cells
sheet.SetStyle(range, style);
// Creating a customized Font object
Font font = new Font("Courier New", 12f);
// Setting the font of range of cells to the customized Font object
sheet.SetFont(range, font);
// Setting the font color of range of cells to Red
sheet.SetFontColor(range, Color.Red);