如何使用C#在Excel范围周围添加边框?

如何使用C#在Excel范围周围添加边框?,c#,excel,vsto,C#,Excel,Vsto,有人能告诉我如何在一系列不同颜色的单元格外添加边框吗?理想情况下,我希望能够做到这一点与一个单一的方法,我将不得不这样做多次。在搜索了这个之后,我找到了两种显然可以做到这一点的方法——BorderAround和BorderAround2。我想我的第一个问题是这两种方法的区别是什么?我试着使用了其中的每一个,但只识别出边界2 总之,'BorderAround2'几乎满足了我的要求。我使用了下面的代码行,在范围外加了一个边框,但它是黑色的,而不是红色的: ws.get_Range("B2", "E3

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

总之,'BorderAround2'几乎满足了我的要求。我使用了下面的代码行,在范围外加了一个边框,但它是黑色的,而不是红色的:

ws.get_Range("B2", "E3").BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexNone, Color.FromArgb(255, 0, 0), Type.Missing);
此方法的示例说明:

必须指定ColorIndex或Color,但不能同时指定两者

我该怎么做呢?如果我将
colorIndex
参数设置为
Type.Missing
null
或完全遗漏,则会产生错误。任何帮助都将不胜感激

最后我要指出的是,我找到了一个解决方案,您可以分别设置不同的边,但正如我所说的,我希望使用一种方法来实现这一点,因为它必须重复多次。

尝试:

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

要将边框添加到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]];
接下来,获取对范围边界数组的引用,如下所示:

Borders border = rowToBottomBorderizeRange.Borders;
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;
最后,将边框指定给边框数组的一条或多条边;例如,如果要在底部添加boder,如下所示:

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”

或者,您可以在如下范围周围添加边框:

Borders border = rowToBottomBorderizeRange.Borders;
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;
注意:您需要这样定义图纸:

Borders border = rowToBottomBorderizeRange.Borders;
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程序集。

谢谢您的回复,但我希望它位于单元格范围之外,而不是每个单元格的周围。这是有效的:ws.get_range(“B2”、“E3”).BorderAround2(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThick,(Excel.XlColorIndex)3,ColorTranslator.ToOle(颜色.红色),类型.缺失);