简体   繁体   English

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

[英]How to add borders around Excel range using C#?

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 . 搜索此内容后,我发现显然可以执行此操作的两个方法BorderAroundBorderAround2 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? 我尝试使用其中的每一个,但只能识别BorderAround2吗?

Anyway, `BorderAround2' almost does what I wanted. 无论如何,“ BorderAround2”几乎可以满足我的要求。 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: 我使用了以下代码行,该代码的确在范围的外部放置了边框,但它是黑色的,而不是红色的:

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. 如果将ColourIndex参数设置为Type.Missing或将其设置为null或完全错过它,则会产生错误。 Any help would be appreciated. 任何帮助,将不胜感激。

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. 最后,我要指出的是,我在这里找到了一种解决方法,可以其中单独设置各个边的集合,但是正如我所说的,我希望使用一种方法来执行此操作,因为它必须重复多次。

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 Range(单元格的范围,通常可以由1 ..很多行和1 ..很多列组成)的一侧或多侧,但是对于这种特定情况,我们可能希望坚持使用一行和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; 最后,将边框分配给Border数组的一个或多个边缘; for example, if you want to add a boder to the bottom, like so: 例如,如果要在底部添加边框,如下所示:

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程序集。

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM