【问题标题】:Excel range Style: specifying Borders via VSTO doesn't workExcel 范围样式:通过 VSTO 指定边框不起作用
【发布时间】:2012-12-11 19:46:31
【问题描述】:

我尝试使用 Excel 样式,而不是通过一一设置单个格式化属性来格式化范围,因为它在格式化大量单元格时似乎更快。我定义了一次样式,然后将其应用于范围,如下所示:

var cell = worksheet.Cells[row, column];
cell.Style = "MyCustomStyle";

它非常适用于内部颜色和字体,但在尝试使用边框时遇到了奇怪的问题。当我尝试定义要在范围上显示哪些边框以及它们应该如何格式化时,我得到了不可预测的结果,并且找不到控制它的方法。

以下方法创建一个名为 ListRowStyle 的 Style;

private static void CreateListRowStyle(Workbook workbook)
{
    var listRowStyle = workbook.Styles.Add(ListRowStyle);

    listRowStyle.Interior.Color = ColorTranslator.ToOle(Color.LightGray);

    listRowStyle.Font.Color = ColorTranslator.ToOle(Color.DarkBlue);
    listRowStyle.Font.Bold = true;

    listRowStyle.IncludeBorder = true;
    listRowStyle.Borders.Color = ColorTranslator.ToOle(Color.Black);
    listRowStyle.Borders.LineStyle = XlLineStyle.xlContinuous;
    listRowStyle.Borders.Weight = XlBorderWeight.xlMedium;
}

这会创建范围内的每一个边框(垂直、水平和对角线) - 到目前为止,一切都很好。但是,当我尝试使用以下代码仅显示顶部和底部边框时,就会出现问题:

private static void CreateEditableListRowStyle(Workbook workbook)
{
    var editableListRowStyle = workbook.Styles.Add(EditableListRowStyle);
    editableListRowStyle.Interior.Color = ColorTranslator.ToOle(Color.Yellow);

    editableListRowStyle.Font.Color = ColorTranslator.ToOle(Color.Red);
    editableListRowStyle.Font.Bold = false;

    editableListRowStyle.IncludeBorder = true;

    editableListRowStyle.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlLineStyleNone;
    editableListRowStyle.Borders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlLineStyleNone;

    editableListRowStyle.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = XlLineStyle.xlLineStyleNone;
    editableListRowStyle.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = XlLineStyle.xlLineStyleNone;

    editableListRowStyle.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
    editableListRowStyle.Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlMedium;

    editableListRowStyle.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
    editableListRowStyle.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThin;
}

颜色样式发生,但没有显示边框。当我修改代码以像这样格式化左右边框时,事情变得更加奇怪:

private static void CreateEditableListRowStyle(Workbook workbook)
{
    var editableListRowStyle = workbook.Styles.Add(EditableListRowStyle);
    editableListRowStyle.Interior.Color = ColorTranslator.ToOle(Color.Yellow);

    editableListRowStyle.Font.Color = ColorTranslator.ToOle(Color.Red);
    editableListRowStyle.Font.Bold = false;

    editableListRowStyle.IncludeBorder = true;

    editableListRowStyle.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
    editableListRowStyle.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlMedium;

    editableListRowStyle.Borders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
    editableListRowStyle.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlMedium;

    editableListRowStyle.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = XlLineStyle.xlLineStyleNone;
    editableListRowStyle.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = XlLineStyle.xlLineStyleNone;

    editableListRowStyle.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
    editableListRowStyle.Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlMedium;

    editableListRowStyle.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
    editableListRowStyle.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThin;
}

此时,顶部和底部边框仍然不显示;另一方面,我得到一个显示的左边框,但没有右边框。呃?

那么 - 我做错了什么,还是通过 VSTO 在样式上设置边框不起作用?请注意,以下代码是 VBA 中 VSTO/C# 代码的非常接近的翻译,其工作方式完全符合我的预期。

Sub Styling()

    ActiveWorkbook.Styles.Add Name:="VbaStyle"

    With ActiveWorkbook.Styles("VbaStyle")
        .IncludeBorder = True
    End With

    ActiveWorkbook.Styles("VbaStyle").Borders(xlLeft).LineStyle = xlNone
    ActiveWorkbook.Styles("VbaStyle").Borders(xlRight).LineStyle = xlNone
    ActiveWorkbook.Styles("VbaStyle").Borders(xlDiagonalDown).LineStyle = xlNone
    ActiveWorkbook.Styles("VbaStyle").Borders(xlDiagonalUp).LineStyle = xlNone

    With ActiveWorkbook.Styles("VbaStyle").Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With

    With ActiveWorkbook.Styles("VbaStyle").Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With

End Sub

这是在 Windows 7、Excel 2007 上。

【问题讨论】:

    标签: excel vba vsto


    【解决方案1】:

    尝试使用 xlLeft、xlRight、xlTop、xlBottom 代替 xlEdgeLeft、xlEdgeRight、xlEdgeTop、xlEdgeBottom

    【讨论】:

      【解决方案2】:

      我尝试了一段时间,遇到了您的问题并得到了一些提示。 感谢那。 能够使用 basedOn 的可选参数创建样式,如下所示:

      var activeSheet = workbook.ActiveSheet as Worksheet;
      Range first = activeSheet.Range["A1"];
      first.Borders.Item[XlBordersIndex.xlEdgeBottom].Color = Color.FromArgb(0, 16, 80);
      first.Borders.Item[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;
      first.Borders.Item[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
      Style myStyle = o9Workbook.Styles.Add("MyStyle",first);
      //reset the first to normal style
      first.Style = "Normal";
      

      希望它对某人有所帮助!

      【讨论】:

        猜你喜欢
        • 2016-05-29
        • 2020-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-28
        • 1970-01-01
        • 2020-05-21
        相关资源
        最近更新 更多