【问题标题】:How to add borders around Excel range using C#?如何使用 C# 在 Excel 范围周围添加边框?
【发布时间】:2015-11-15 09:35:41
【问题描述】:

有人能告诉我如何在一系列单元格的外部添加另一种颜色的边框吗?理想情况下,我希望能够用一种方法来做到这一点,我将不得不多次这样做。在搜索了这个之后,我发现了两种显然可以做到这一点的方法 - BorderAroundBorderAround2。我想我的第一个问题是这两种方法有什么区别?我尝试使用其中的每一个,但只有 BorderAround2 被识别?

无论如何,`BorderAround2' 几乎可以满足我的要求。我使用了以下代码行,它确实在范围之外设置了一个边框,但它是黑色的,而不是红色的:

ws.get_Range("B2", "E3").BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexNone, Color.FromArgb(255, 0, 0), Type.Missing);

此方法的MSDN documentation 声明:

您必须指定 ColorIndex 或 Color,但不能同时指定。

我该怎么做呢?如果我将 ColourIndex 参数设置为 Type.Missingnull 或完全错过它,则会产生错误。任何帮助将不胜感激。

最后我应该指出,我找到了一种解决方法here,您可以在其中分别设置各种边缘,但正如我所说,我希望使用单一方法来执行此操作,因为它必须重复多次.

【问题讨论】:

    标签: c# excel vsto


    【解决方案1】:

    要在 Excel 范围(单元格范围,通常由 1.. 多行和 1.. 多列组成)的一侧或多侧添加边框,但对于这种特定情况,我们可能希望坚持一行1..多列),你只需要做三件事:

    0) Define the range
    1) Get a reference to the Range's Borders array
    2) Assign a border to one or more of the Border array's edges/sides (top, bottom, left, right)
    

    首先,定义您想要操作的范围,如下所示:

    var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
    

    接下来,获取 Range 的 Borders 数组的引用,如下所示:

    Borders border = rowToBottomBorderizeRange.Borders;
    

    最后,为 Border 数组的一条或多条边分配一个边框;例如,如果您想在底部添加边框,如下所示:

    border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
    

    把它们放在一起,代码可能是:

    var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
    Borders border = rowToBottomBorderizeRange.Borders;
    border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
    

    如果你在几个地方这样做,你可以用它做一个方法:

    private void AddBottomBorder(int rowToBottomBorderize)
    {
        var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
        Borders border = rowToBottomBorderizeRange.Borders;
        border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
    }
    

    上面的示例仅显示了添加底部边框,但您可以同样轻松地添加顶部、左侧或右侧边框线,将“xlEdgeBottom”替换为“xlEdgeTop”、“xlEdgeRight”或“xlEdgeLeft”

    或者,您可以在这样的范围内添加边框:

    private void Add360Borders(int rowToBorderize)
    {
        var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBorderize, TOTALS_COL]];
        Borders border = rowToBorderizeRange.Borders;
        border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
        border[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
        border[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
        border[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
    }
    

    注意:您需要像这样定义工作表:

    private Worksheet _xlSheet;
    

    ...并在您的解决方案中引用 Microsoft.Office.Interop.Excel 程序集。

    【讨论】:

      【解决方案2】:

      试试:

      ws.get_Range("B2", "E3").Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
      ws.get_Range("B2", "E3").Borders.Color = ColorTranslator.ToOle(Color.Red);
      

      【讨论】:

      • 感谢您的回复,但我希望它在单元格范围之外,而不是在每个单独的单元格周围。
      • 这有效:ws.get_Range("B2", "E3").BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, (Excel.XlColorIndex)3, ColorTranslator.ToOle(Color .Red), Type.Missing);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 2016-12-02
      相关资源
      最近更新 更多