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

发布于 2024-08-21 18:19:18 字数 355 浏览 10 评论 0原文

这是我正在使用的代码:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

写给空气的情书 2024-08-28 18:19:18

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

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

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.

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!

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

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.

梦醒灬来后我 2024-08-28 18:19:18

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

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

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));
离笑几人歌 2024-08-28 18:19:18
worksheet.Cells[8, i].Borders.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
worksheet.Cells[8, i].Borders.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
舟遥客 2024-08-28 18:19:18
.Range("H1:J1").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, color:=RGB(130, 130, 130)
.Range("H1:J1").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, color:=RGB(130, 130, 130)
匿名。 2024-08-28 18:19:18
range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;

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

range.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);
假面具 2024-08-28 18:19:18

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

Color:=RGB(255, 0, 0)

以及您感兴趣的 RGB 代码,或者
ColorIndex:=3 - 例如,获取红色。

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

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

To change the border color you have to use either

Color:=RGB(255, 0, 0)

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

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].

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文