用C++设置边框

向单元格添加边框

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

借助 Aspose.Cells,开发人员可以以与 Microsoft Excel 中相同的灵活方式添加边框并自定义其外观。

向单元格添加边框

Aspose.Cells提供了Workbook类,代表一个Microsoft Excel文件。Workbook类包含一个Worksheets集合,可访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了Cells集合。Cells集合中的每个元素都代表一个Cell类的对象。

Aspose.Cells在Cell类中提供了GetStyle方法,用于设置单元格的格式样式。Style类还提供了添加边框的属性。

向单元格添加边框

开发者可以通过使用Style对象的GetBorders()集合为单元格添加边框。边框类型作为索引传入GetBorders()集合。所有边框类型都在BorderType枚举中预定义。

边框枚举

边框类型 描述
BottomBorder 底部边框线
DiagonalDown 从左上到右下的斜线
DiagonalUp 从左下到右上的斜线
LeftBorder 左边框线
RightBorder 右边框线
TopBorder 顶部边框线

GetBorders()集合存储所有边框。GetBorders()集合中的每个边框由Border对象表示,提供两个属性,GetColor()GetLineStyle(),分别设置边框的线色和线样式。

要设置边框的线色,可以使用Color枚举选择颜色,并将其赋值给Border对象的Color属性。

通过从 CellBorderType 枚举中选择线条样式来设置边框的线条样式。

CellBorderType枚举

线条样式 说明
DashDot 细点划线
DashDotDot 细点点划线
Dashed 虚线
Dotted 点线
Double 双线
Hair 细线
MediumDashDot 中等点划线
MediumDashDotDot 中等点点划线
MediumDashed 中等虚线
None 无线
Medium 中等线
SlantedDashDot 斜点划线(中等)
Thick 厚线
Thin 薄线

选择一种线条样式,然后将其分配给 Border 对象的 GetLineStyle() 属性。

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    Workbook workbook;
    Worksheet worksheet = workbook.GetWorksheets().Get(0);
    Cell cell = worksheet.GetCells().Get(u"A1");
    cell.PutValue(u"Visit Aspose!");

    Style style = cell.GetStyle();

    style.GetBorders().Get(BorderType::TopBorder).SetLineStyle(CellBorderType::Thick);
    style.GetBorders().Get(BorderType::TopBorder).SetColor(Color::Black());

    style.GetBorders().Get(BorderType::BottomBorder).SetLineStyle(CellBorderType::Thick);
    style.GetBorders().Get(BorderType::BottomBorder).SetColor(Color::Black());

    style.GetBorders().Get(BorderType::LeftBorder).SetLineStyle(CellBorderType::Thick);
    style.GetBorders().Get(BorderType::LeftBorder).SetColor(Color::Black());

    style.GetBorders().Get(BorderType::RightBorder).SetLineStyle(CellBorderType::Thick);
    style.GetBorders().Get(BorderType::RightBorder).SetColor(Color::Black());

    cell.SetStyle(style);

    workbook.Save(outDir + u"book1.out.xls");
    std::cout << "Excel file saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

向单元格范围添加边框

也可以对一范围单元格添加边框,而不仅仅是单个单元格。首先,通过调用 Cells 集合的 CreateRange 方法创建一个单元格范围。它接受以下参数:

  • 第一行,范围的第一行。
  • 第一列,表示范围的第一列。
  • 行数,范围中的行数。
  • 列数,范围中的列数。

CreateRange 方法返回一个 Range 对象,其中包含指定范围的单元格。Range 对象提供一个 SetOutlineBorder 方法,可以接受以下参数,为单元格范围添加边框:

  • 边框类型,选择自 BorderType 枚举的边框类型。
  • 线条样式,选择自 CellBorderType 枚举的边框线条样式。
  • 颜色,线条颜色,从Color枚举中选择。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Create workbook
    Workbook workbook;

    // Obtain the reference of the first (default) worksheet
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Accessing the "A1" cell from the worksheet
    Cell cell = worksheet.GetCells().Get(u"A1");

    // Adding some value to the "A1" cell
    cell.PutValue(u"Hello World From Aspose");

    // Creating a range of cells starting from "A1" cell to 3rd column in a row
    Range range = worksheet.GetCells().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(outDir + u"book1.out.xls");

    std::cout << "Excel file created successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}