[英]How to programmatically add colored border to a cell in C# Excel vsto?
How can I add a colored border to a cell in C# Excel VSTO? 如何在C#Excel VSTO中为单元格添加彩色边框?
I found the API via the following link, and there're some descriptions regarding adding borders, but not very specific. 我通过以下链接找到了API,并且有关于添加边框的一些描述,但不是非常具体。 The weird thing is VS2010 doesn't recognize
BorderAround()
method. 奇怪的是VS2010无法识别
BorderAround()
方法。 It seems to only recognize BorderAround2()
, but complaining the arguments I put int. 它似乎只识别
BorderAround2()
,但抱怨我把int放入的参数。
Below is the code that I tried, but VS complains about the invalid arguments. 下面是我尝试的代码,但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 http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.borderaround(v=office.14).aspx
Use the Borders.Color
and Bordes.LineStyle
properties of the Range
object. 使用
Range
对象的Borders.Color
和Bordes.LineStyle
属性。
This is a snippet from a VSTO application-level addin. 这是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;
Note that the Borders.Color
property uses the inverse of the supplied RGB value as the color. 请注意,
Borders.Color
属性使用提供的RGB值的倒数作为颜色。
Alternatively, you could use the ColorIndex
property, but the range of colors is limited. 或者,您可以使用
ColorIndex
属性,但颜色范围有限。 See this for more details. 有关详细信息,请参阅此
My own approach is similar to what idssl suggested, but utilizing ColorTranslator.ToOle method. 我自己的方法类似于idssl建议,但使用ColorTranslator.ToOle方法。
range.Borders.LineStyle = Excel.XlLineStyle.xlDot;
range.Borders.Color = ColorTranslator.ToOle(Color.Red);
Which also works for me. 这对我也有用。
You should see here 你应该看到这里
http://www.aspose.com/docs/display/cellsnet/Add+Borders+to+Cells+in+a+Worksheet http://www.aspose.com/docs/display/cellsnet/Add+Borders+to+Cells+in+a+Worksheet
With 同
//Set the borders with hair lines style.
_range.SetOutlineBorders( CellBorderType.Hair, Color.Black);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.