格式化一系列单元格
Contents
[
Hide
]
这个主题也属于与应用字体设置和其他格式样式相关的系列主题。我们之前的主题已经很好地涵盖了处理这些特性。例如,你可以参考 更改单元格的字体和颜色 和 对单元格应用样式 主题来了解相同的特性。那么如果我们已经涵盖了这些概念,这个主题有什么新内容。嗯,这个主题与之前的不同之处在于我们将对一系列单元格应用格式设置(涉及字体和其他样式),而不仅仅是一个单个单元格。我们希望你仍然会发现这个主题对你有用。
设置一系列单元格的字体和样式
在我们讨论格式设置之前(我们在之前的主题中已经谈过很多),我们应该了解如何创建一系列单元格。嗯,我们可以使用CellRange类来创建一系列单元格,它的构造函数接受一些参数来指定单元格的范围。我们可以使用Names或行列索引来指定起始和结束单元格的范围。
一旦我们创建了CellRange对象,我们就可以使用Worksheet的重载版本的SetStyle、SetFont和SetFontColor方法,这些方法可以接受一个CellRange对象来应用指定范围的单元格上的格式设置。
让我们通过一个例子来了解这个基本概念。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |