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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2265342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:52:34  来源:igfitidea点击:

Excel Range.BorderAround(), Border is always black

c#export-to-excel

提问by Aseem Gautam

This is the code I am using:

这是我正在使用的代码:

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)));

The border color is always black no matter what RGB value I provide.

无论我提供什么 RGB 值,边框颜色始终为黑色。

采纳答案by Wad

I had the same problem, couldn't find any solution on the web, MS documentation for use of this method in VSTO is a bit poor.

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

Anyway, probably a bit late for you seeing as you posted months ago, but my workaround was simply to not use the Range.BorderAround method and write my own!

无论如何,您看到几个月前发布的内容可能有点晚了,但我的解决方法是不使用 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;
    }

Can be invoked as per below example (Contents_Table is a NamedRange in my worksheet):

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

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

Hope this helps someone else out there tearing their hair out.

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

回答by JAIRO

.Range("H1:J1").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, color:=RGB(130, 130, 130)

回答by Barry Kaye

Alternatively if you're not worried to ensure the removal of inside and diagonal lines I have successfully used:

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

range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(153, 153, 153));

回答by Neptunus

worksheet.Cells[8, i].Borders.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 

回答by Sharique Ansari

range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;

range.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);

回答by Licu

To change the border color you have to use either

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

Color:=RGB(255, 0, 0)

Color:=RGB(255, 0, 0)

with the RGB code you're interesting in, or ColorIndex:=3- to get the red color, for instance.

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

If you use both, [ColorIndex:=3]will override [Color:=RGB(255, 0, 0)]- action visible when you try to set different colors for each, or use [colorindex:=xlColorIndeAutomatic]or with [xlColorIndexNone].

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

Unlike in Excel formulas, in VBA you can and probably should skip parameters as they are suggested by VBA's intellisense...

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