如何以编程方式向c#excel vsto中的单元格添加彩色边框?

有谁知道如何添加彩色边框到C#Excel VSTO中的单元格? 任何好的例子将不胜感激。 谢谢。

编辑:我有点通过以下链接findAPI,并有一些关于添加边框的说明,但不是非常具体。 奇怪的是VS2010不能识别BorderAround()方法。 它似乎只承认BorderAround2() ,但抱怨我把int的参数。 下面是我尝试的代码,但VS抱怨无效的参数。

 range.BorderAround2(Excel.XlLineStyle.xlDash, Type.Missing, Type.Missing, System.Drawing.Color.Red, Type.Missing); 

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.borderaround(v=office.14).aspx

使用Range对象的Borders.ColorBordes.LineStyle属性。

这是来自VSTO应用程序级插件的片段。

 using Excel = Microsoft.Office.Interop.Excel; Excel.Range pRange = Globals.ThisAddIn.Application.ActiveCell; pRange.Borders.Color = 0x0000FF; // an RGB value in hex pRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; 

请注意, Borders.Color属性使用提供的RGB值的倒数作为颜色。

或者,您可以使用ColorIndex属性,但颜色的范围是有限的。 看到这个更多的细节。

我自己的方法类似于idssl所build议的,但是使用了ColorTranslator.ToOle方法。

 range.Borders.LineStyle = Excel.XlLineStyle.xlDot; range.Borders.Color = ColorTranslator.ToOle(Color.Red); 

这也适用于我。

你应该看到这里

http://www.aspose.com/docs/display/cellsnet/Add+Borders+to+Cells+in+a+Worksheet

 //Set the borders with hair lines style. _range.SetOutlineBorders( CellBorderType.Hair, Color.Black);