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

时间:2022-11-02 20:23:06

Could somebody tell me how to add borders around the outside of a range of cells in another colour? Ideally I would like to be able to do this with a single method I will have to do this multiple times. After searching for this I found two methods that would apparently do this - BorderAround and BorderAround2. I suppose my first question is what is the difference between these two methods? I tried using each of these and only BorderAround2 was recognised?

有人能告诉我如何在另一种颜色的一系列细胞外面添加边框吗?理想情况下,我希望能够通过一种方法完成此操作,我将不得不多次执行此操作。在搜索完之后,我发现了两种显然会这样做的方法 - BorderAround和BorderAround2。我想我的第一个问题是这两种方法有什么区别?我试过使用这些中的每一个并且只识别了BorderAround2?

Anyway, `BorderAround2' almost does what I wanted. I used the following line of code which did put a border around the outside of the range, but it was black, rather than red:

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

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

The MSDN documentation for this method states:

此方法的MSDN文档指出:

You must specify either ColorIndex or Color, but not both.

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

How do I go about doing this? If I set the ColourIndex parameter to Type.Missing or to null or miss it out completely, it produces an error. Any help would be appreciated.

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

Finally I should point out that I found a workaround solution here where you set the set the various edges separately, but as I say, I was hoping to do this using a single method as it has to be repeated multiple times.

最后我应该指出,我在这里找到了一个解决方案解决方案,你可以分别设置各个边缘,但正如我所说,我希望使用单个方法来完成这个,因为它必须重复多次。

2 个解决方案

#1


2  

To add a border to one or more sides of an Excel Range (range of cells, which can normally be comprised of 1..many rows and 1..many columns, but for this specific scenario, we probably want to stick with one row and 1..many columns), you only need do three things:

要向Excel范围的一个或多个边添加边框(单元格范围,通常可以包含1..many行和1..many列,但对于此特定方案,我们可能希望坚持使用一行和1..many专栏),你只需要做三件事:

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)

First, define the range over which you want to operate on like so:

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

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

Next, obtain a reference to the Range's Borders array like this:

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

Borders border = rowToBottomBorderizeRange.Borders;

Finally, assign a border to one or more of the Border array's edges; for example, if you want to add a boder to the bottom, like so:

最后,为一个或多个Border数组的边指定边框;例如,如果要将boder添加到底部,如下所示:

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

Putting it all together, the code could be:

总而言之,代码可以是:

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

If you do this in several places, you could make a method out of it:

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

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;
}

The example above shows just adding a bottom border, but you can add top, left, or right border lines just as easily, replacing "xlEdgeBottom" with "xlEdgeTop", "xlEdgeRight", or "xlEdgeLeft"

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

Or, you could add borders all around a range like this:

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

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;
}

Note: You will need to define the sheet like this:

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

private Worksheet _xlSheet;

...and reference the Microsoft.Office.Interop.Excel assembly in your solution.

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

#2


1  

Try:

尝试:

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

#1


2  

To add a border to one or more sides of an Excel Range (range of cells, which can normally be comprised of 1..many rows and 1..many columns, but for this specific scenario, we probably want to stick with one row and 1..many columns), you only need do three things:

要向Excel范围的一个或多个边添加边框(单元格范围,通常可以包含1..many行和1..many列,但对于此特定方案,我们可能希望坚持使用一行和1..many专栏),你只需要做三件事:

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)

First, define the range over which you want to operate on like so:

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

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

Next, obtain a reference to the Range's Borders array like this:

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

Borders border = rowToBottomBorderizeRange.Borders;

Finally, assign a border to one or more of the Border array's edges; for example, if you want to add a boder to the bottom, like so:

最后,为一个或多个Border数组的边指定边框;例如,如果要将boder添加到底部,如下所示:

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

Putting it all together, the code could be:

总而言之,代码可以是:

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

If you do this in several places, you could make a method out of it:

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

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;
}

The example above shows just adding a bottom border, but you can add top, left, or right border lines just as easily, replacing "xlEdgeBottom" with "xlEdgeTop", "xlEdgeRight", or "xlEdgeLeft"

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

Or, you could add borders all around a range like this:

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

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;
}

Note: You will need to define the sheet like this:

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

private Worksheet _xlSheet;

...and reference the Microsoft.Office.Interop.Excel assembly in your solution.

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

#2


1  

Try:

尝试:

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