aardio 文档

dotNet.ReoGrid 扩展库 - 边框设置

边框可以通过编程方式设置,直接调用工作表API,也可以通过执行操作来设置。

边框样式

当前版本的ReoGrid支持以下边框样式:

264

边框样式定义为BorderLineStyle枚举。

边框位置

边框必须围绕一个区域设置,即使该区域只包含一个单元格。要指定边框位置,请使用BorderPositions枚举及其二进制组合值。

266

设置边框

要在工作表中为特定区域应用边框,使用SetRangeBorders方法:

import dotNet.ReoGrid;
var sheet1 = reoGridControl.CurrentWorksheet;
// 为指定区域外部应用自定义样式的边框
sheet1.SetRangeBorders("B3:D7", ReoGrid.BorderPositions.Outside, ReoGrid.RangeBorderStyle(
    Color = ReoGrid.Graphics.SolidColor.Magenta,
    Style = ReoGrid.BorderLineStyle.BoldSolid
));

18_2

通过操作设置边框

要通过可撤销的操作在区域内应用边框,使用以下方法:

reoGridControl.DoAction(reoGridControl.CurrentWorksheet, 
    ReoGrid.Actions.SetRangeBorderAction(
        ReoGrid.RangePosition(2, 1, 5, 3),
        ReoGrid.BorderPositions.All,
        ReoGrid.RangeBorderStyle({
            Color = ReoGrid.Graphics.SolidColor.Red,
            Style = ReoGrid.BorderLineStyle.Dashed
        })
    )
);

19_2

获取边框信息

要获取工作表中特定区域应用的边框详细信息,使用GetRangeBorders方法:

// 获取指定区域的边框信息
var borderInfoSet = sheet1.GetRangeBorders("A1:D5");

GetRangeBorders方法分析指定区域内的边框并返回一个RangeBorderInfoSet。该集合包含在整个区域内一致应用的边框样式实例。

要识别区域内样式不同的边框,检查RangeBorderInfoSetNonUniformPos属性。该属性突出显示与主要样式不同的边框位置。

遍历边框

要枚举指定区域内的所有边框,使用工作表的IterateBorders方法。

方法定义

IterateBorders(RowOrColumn direction, RangePosition range, Func<int, int, int, RangeBorderStyle, bool> iterator);

参数

例如,遍历所有水平边框,如果单元格位置有边框,则将单元格背景标记为红色:

// 在指定区域内设置水平内部边框
sheet1.Ranges["C3:F10"].BorderInsideHorizontal = ReoGrid.RangeBorderStyle.BlackSolid;
// 在整个工作表区域内迭代水平边框
sheet1.IterateBorders(ReoGrid.RowOrColumn.Row, ReoGrid.RangePosition.EntireRange, function(r, c, span, border) {
    // 为有边框的单元格应用红色背景
    sheet1.Cells[r, c].Style.BackColor = ReoGrid.Graphics.SolidColor.LightCoral;
    return true; // 继续迭代
});

277

迭代所有垂直边框:

// 设置边框
sheet1.Ranges["C3:F10"].BorderInsideVertical = ReoGrid.RangeBorderStyle.BlackSolid;
// 迭代边框
sheet1.IterateBorders(ReoGrid.RowOrColumn.Column, ReoGrid.RangePosition.EntireRange, function(r, c, span, border) {
    sheet1.Cells[r, c].Style.BackColor = ReoGrid.Graphics.SolidColor.LightCoral;
    return true;
});

278

迭代所有边框,包括行和列边框:

// 设置边框
sheet1.Ranges["C3:F10"].BorderOutside = ReoGrid.RangeBorderStyle.BlackBoldSolid;
sheet1.Ranges["C3:F10"].BorderInsideHorizontal = ReoGrid.RangeBorderStyle.BlackSolid;
sheet1.Ranges["C3:F10"].BorderInsideVertical = ReoGrid.RangeBorderStyle.BlackDotted;
// 迭代边框
sheet1.IterateBorders(ReoGrid.RowOrColumn.Both, ReoGrid.RangePosition.EntireRange, function(r, c, span, border) {
    sheet1.Cells[r, c].Style.BackColor = ReoGrid.Graphics.SolidColor.LightCoral;
    return true;
});

279

移除边框

要从工作表中移除边框,可以使用RemoveRangeBorders方法:

// 从工作表中移除指定区域的所有边框
sheet1.RemoveRangeBorders(ReoGrid.RangePosition(2, 1, 5, 1), ReoGrid.BorderPositions.All);

或者,将边框样式设置为RangeBorderStyle.Empty也可以有效移除边框:

// 将指定区域的边框样式设置为'Empty'以移除边框
sheet1.SetRangeBorders(ReoGrid.RangePosition(2, 1, 5, 3), ReoGrid.BorderPositions.All, ReoGrid.RangeBorderStyle.Empty);

通过操作移除边框

要通过可撤销的操作移除边框,可以如下使用DoAction方法:

reoGridControl.DoAction(reoGridControl.CurrentWorksheet,
    ReoGrid.Actions.RemoveRangeBorderAction(
        ReoGrid.RangePosition(2, 1, 5, 1), 
        ReoGrid.ReoGridBorderPos.All
    )
);

可以通过调用Undo方法撤销此操作:

// 撤销边框移除操作
reoGridControl.Undo();

Markdown 格式