边框设置

为 Cells 添加边框

Microsoft Excel 允许用户通过添加边框来格式化单元格。边框的类型取决于添加的位置。例如,顶部边框是添加到单元格顶部位置的边框。用户还可以修改边框的线条样式和颜色。

使用 Aspose.Cells,开发人员可以像在 Microsoft Excel 中一样灵活地添加边框和自定义外观。

为 Cells 添加边框

Aspose.Cells提供了一个类,工作簿表示 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells收藏。中的每一项Cells集合代表一个对象Cell班级。

Aspose.Cells 提供了获取样式中的方法Cell班级。这设置样式方法用于设置单元格的格式样式。这风格类提供用于向单元格添加边框的属性。

为 Cell 添加边框

开发人员可以使用风格对象的边框收藏。边框类型作为索引传递给边框收藏。所有边框类型都预定义在边框类型枚举。

边界枚举

边框类型 描述
底部边框 底部边界线
对角向下 从左上角到右下角的对角线
对角向上 从左下角到右上角的对角线
左边框 一条左边界线
右边框 右边界线
顶部边框 顶部边界线

边框集合存储所有边界。中的每个边框边框集合由一个表示边界提供两个属性的对象,颜色线型分别设置边框的线条颜色和样式。

要设置边框的线条颜色,请使用 Color 枚举(.NET Framework 的一部分)选择一种颜色,并将其分配给 Border 对象的 Color 属性。

边框的线条样式是通过从中选择一种线条样式来设置的单元格边框类型枚举。

CellBorderType 枚举

线型 描述
点划线 细点划线
点点滴滴 细点划线
虚线 虚线
点缀的 虚线
双倍的 双线
头发 发际线
MediumDashDot 中点划线
MediumDashDotDot 点 中点划线
中虚线 中虚线
没有任何 没有线
中等的 中线
斜划线点 倾斜的中点划线
厚的 粗线
薄的 细线
选择一种线型,然后将其分配给边界对象的线型财产。
// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Create a style object
Style style = cell.GetStyle();
// Setting the line style of the top border
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick;
// Setting the color of the top border
style.Borders[BorderType.TopBorder].Color = Color.Black;
// Setting the line style of the bottom border
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick;
// Setting the color of the bottom border
style.Borders[BorderType.BottomBorder].Color = Color.Black;
// Setting the line style of the left border
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick;
// Setting the color of the left border
style.Borders[BorderType.LeftBorder].Color = Color.Black;
// Setting the line style of the right border
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick;
// Setting the color of the right border
style.Borders[BorderType.RightBorder].Color = Color.Black;
// Apply the border styles to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

为 Cells 范围添加边框

也可以为一系列单元格添加边框,而不仅仅是单个单元格。为此,首先,通过调用Cells收藏的创建范围方法。它采用以下参数:

  • First Row,范围的第一行。
  • 第一列,代表范围的第一列。
  • Number of Rows,范围内的行数。
  • Number of Columns,范围内的列数。

创建范围方法返回一个范围对象,其中包含指定范围的单元格。这范围对象提供了一个设置轮廓边框采用以下参数为单元格区域添加边框的方法:

  • 边框类型,边框类型,从边框类型枚举。
  • 线型 边框线条样式,选自单元格边框类型枚举。
  • 颜色,线条颜色,从 Color 枚举中选择。
// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello World From Aspose");
// Creating a range of cells starting from "A1" cell to 3rd column in a row
Range range = worksheet.Cells.CreateRange(0, 0, 1, 3);
// Adding a thick top border with blue line
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);
// Adding a thick bottom border with blue line
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);
// Adding a thick left border with blue line
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);
// Adding a thick right border with blue line
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

颜色和调色板

调色板是可用于创建图像的颜色数量。在演示文稿中使用标准化调色板允许用户创建一致的外观。每个 Microsoft Excel (97-2003) 文件都有一个 56 种颜色的调色板,可应用于图表中的单元格、字体、网格线、图形对象、填充和线条。

使用 Aspose.Cells 不仅可以使用调色板的现有颜色,还可以使用自定义颜色。在使用自定义颜色之前,请先将其添加到调色板中。

本主题讨论如何将自定义颜色添加到调色板。

将自定义颜色添加到调色板

Aspose.Cells 支持Microsoft Excel的56色调色板。要使用未在调色板中定义的自定义颜色,请将颜色添加到调色板。

Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类提供了改变调色板采用以下参数添加自定义颜色以修改调色板的方法:

  • Custom Color,要添加的自定义颜色。
  • 索引,自定义颜色将替换的调色板中颜色的索引。应该在 0-55 之间。

下面的示例在将自定义颜色(兰花)应用于字体之前将其添加到调色板。

// Instantiating an Workbook object
Workbook workbook = new Workbook();
//Checks if a color is in the palette for the spreadsheet.
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding Orchid color to the palette at 55th index
workbook.ChangePalette(Color.Orchid, 55);
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// 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
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining new Style object
Style styleObject = workbook.CreateStyle();
// Setting the Orchid (custom) color to the font
styleObject.Font.Color = workbook.Colors[55];
// Applying the style to the cell
cell.SetStyle(styleObject);
// Saving the Excel file
workbook.Save("out.xlsx");