個々の書式設定プロパティを1つずつ設定して範囲を書式設定する代わりに、Excelスタイルを使用しようとしています。これは、多数のセルを書式設定する方が速いように思われるためです。スタイルを一度定義してから、次のように範囲に適用します。
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
これはWindows7、Excel2007にあります。