Excel范围样式:通过VSTO指定边框不起作用

通过逐个设置个别格式化属性而不是格式化范围,我试图使用Excel样式,因为格式化大量单元格似乎更快。 我定义一个样式,然后将其应用到这样的范围:

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

它适用于室内的颜色和字体,但我遇到奇怪的问题,当试图与边界工作。 当我尝试定义在范围上显示哪些边界,以及如何对其进行格式化时,会得到不可预知的结果,并且无法find控制它的方法。

下面的方法创build一个名为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; } 

这创造了范围内的每一个边界(垂直,水平和对angular线) – 到目前为止,这么好。 但是,当我尝试仅显示顶部和底部边框时,使用以下代码,问题开始发生:

 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中。

尝试使用xlLeft,xlRight,xlTop,xlBottom而不是xlEdgeLeft,xlEdgeRight,xlEdgeTop,xlEdgeBottom

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

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

希望它可以帮助别人!