边框设置
向单元格添加边框
Microsoft Excel 允许用户通过添加边框来格式化单元格。边框的类型取决于添加的位置。例如,顶部边框是添加到单元格顶部位置的边框。用户还可以修改边框的线条样式和颜色。
借助 Aspose.Cells,开发人员可以以与 Microsoft Excel 中相同的灵活方式添加边框并自定义其外观。
向单元格添加边框
Aspose.Cells 提供了一个表示 Microsoft Excel 文件的 Workbook 类。 Workbook 类包含一个 Worksheets 集合,该集合允许访问 Excel 文件中的每个工作表。工作表由 Worksheet 类表示。 Worksheet 类提供 Cells 集合。 Cells 集合中的每个项表示 Cell 类的对象。
Aspose.Cells 在 Cell 类中提供了 GetStyle 方法。 SetStyle 方法用于设置单元格的格式样式。 Style 类提供了用于向单元格添加边框的属性。
向单元格添加边框
开发人员可以通过使用 Style 对象的 Borders 集合向单元格添加边框。边框类型作为索引传递给 Borders 集合。所有边框类型都在 BorderType 枚举中预定义。
边框枚举
边框类型 | 描述 |
---|---|
BottomBorder | 底部边框线 |
DiagonalDown | 从左上到右下的对角线 |
DiagonalUp | 从左下到右上的对角线 |
LeftBorder | 左边框线 |
RightBorder | 右边框线 |
TopBorder | 顶部边框线 |
The Borders collection stores all borders. Each border in the Borders collection is represented by a Border object which provides two properties, Color and LineStyle to set a border’s line color and style respectively.
要设置边框线颜色,请使用.NET Framework的Color枚举(.NET Framework的一部分)选择颜色并将其分配给Border对象的Color属性。
通过从CellBorderType枚举中选择线条样式来设置边框线样式。
CellBorderType枚举
线条样式 | 描述 |
---|---|
DashDot | 细长虚点线 |
DashDotDot | 细长虚点虚点线 |
Dashed | 虚线 |
Dotted | 点状线 |
Double | 双线 |
Hair | 细线 |
MediumDashDot | 中等虚点线 |
MediumDashDotDot | 中等虚点虚点线 |
MediumDashed | 中等虚线 |
None | 无线 |
Medium | 中线 |
SlantedDashDot | 倾斜中等虚点线 |
Thick | 粗线 |
Thin | 细线 |
选择其中一种线条样式,然后将其分配给“Border”对象的LineStyle属性。 |
// 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”集合的CreateRange方法创建一系列单元格。它使用以下参数:
- 第一行,范围的第一行。
- 第一列,表示范围的第一列。
- 行数,范围中的行数。
- 列数,范围中的列数。
“CreateRange”方法返回一个Range对象,它包含指定范围的单元格。Range对象提供一个SetOutlineBorder方法,使用以下参数为单元格范围添加边框:
- 边框类型,从BorderType枚举中选择的边框类型。
- 线条样式,从CellBorderType枚举中选择的边框线条样式。
- 颜色,线条颜色,从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"); |