23

这是我正在使用的代码:

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 值,边框颜色始终为黑色。

4

6 回答 6

58

我有同样的问题,在网上找不到任何解决方案,在 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)));

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

于 2010-06-23T10:37:13.310 回答
8

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

range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(153, 153, 153));
于 2011-01-04T17:21:36.197 回答
5
worksheet.Cells[8, i].Borders.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
于 2012-04-07T19:12:25.293 回答
3
.Range("H1:J1").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, color:=RGB(130, 130, 130)
于 2010-08-19T16:52:41.747 回答
1
range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;

range.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);
于 2013-01-11T10:42:18.327 回答
-1

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

Color:=RGB(255, 0, 0)

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

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

与 Excel 公式不同,在 VBA 中,您可以并且可能应该跳过 VBA 的智能感知所建议的参数......

于 2018-01-25T22:02:49.050 回答