我没有通过一一设置单个格式属性来设置范围,而是尝试使用 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 上。