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

mik*_*ike 6 c# excel vsto

有人能告诉我如何在另一种颜色的一系列细胞外面添加边框吗?理想情况下,我希望能够通过一种方法完成此操作,我将不得不多次这样做.在寻找这个之后,我找到了两种显然会这样做的方法 - 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);
Run Code Online (Sandbox Code Playgroud)

此方法的MSDN文档指出:

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

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

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

Ric*_*ell 2

尝试:

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

  • 这有效: ws.get_Range("B2", "E3").BorderAround2(Excel.XlLineStyle.xlContinously, Excel.XlBorderWeight.xlThick, (Excel.XlColorIndex)3, ColorTranslator.ToOle(Color.Red), Type.Missing) ; (3认同)