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

Ase*_*tam 21 c# export-to-excel

这是我正在使用的代码:

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)));
Run Code Online (Sandbox Code Playgroud)

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

小智 56

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

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

BorderAround(Contents_Table.Cells, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(79, 129, 189)));
Run Code Online (Sandbox Code Playgroud)

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


Bar*_*aye 8

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

range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(153, 153, 153));
Run Code Online (Sandbox Code Playgroud)


小智 5

worksheet.Cells[8, i].Borders.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
Run Code Online (Sandbox Code Playgroud)