不是通過逐個設置各個格式屬性來設置範圍,而是嘗試使用Excel樣式,因爲它在格式化大量單元格時似乎更快。我定義了一個樣式一次,然後將其應用於範圍是這樣的:Excel範圍樣式:通過VSTO指定邊框不起作用
var cell = worksheet.Cells[row, column];
cell.Style = "MyCustomStyle";
它完美的內飾顏色和字體,但我想有邊界工作的時候遇到了奇怪的問題。當我嘗試定義在範圍上顯示哪些邊界以及如何對其進行格式化時,我會得到不可預測的結果,並且無法找到控制它的方法。
以下方法創建一個名爲ListRowStyle的樣式;
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中