Excel Range.BorderAround(),边框始终为黑色

这是我正在使用的代码:

rngData.BorderAround(Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexNone, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(178, 178, 178))); 

无论我提供什么RGB值,边框颜色总是黑色。

我有同样的问题,在网上找不到任何解决方案,在VSTO中使用此方法的MS文档有点差。

无论如何,你几个月前发布的时间可能有点晚了,但我的解决办法就是不使用Range.BorderAround方法并编写我自己的方法!

  private void BorderAround(Excel.Range range, int colour) { Excel.Borders borders = range.Borders; borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous; borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous; borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous; borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous; borders.Color = colour; borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders = null; } 

可以按照以下示例调用(Contents_Table是我的工作表中的NamedRange):

 BorderAround(Contents_Table.Cells, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(79, 129, 189))); 

希望这可以帮助其他人撕掉他们的头发。

或者,如果您不担心确保删除我已成功使用的内部和对角线:

 range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; range.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(153, 153, 153)); 
 worksheet.Cells[8, i].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
 .Range("H1:J1").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, color:=RGB(130, 130, 130) 
 range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; range.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red); 

要更改边框颜色,您必须使用

Color:=RGB(255, 0, 0)

例如,使用你感兴趣的RGB代码,或者ColorIndex:=3 – 来获得红色。

如果同时使用两者, [ColorIndex:=3]将覆盖[Color:=RGB(255, 0, 0)] – 当您尝试为每个颜色设置不同的颜色时,操作可见,或使用[colorindex:=xlColorIndeAutomatic][xlColorIndexNone]

与Excel公式不同,在VBA中,您可以并且可能应该跳过参数,因为它们是由VBA的intellisense建议的……