※本文は Excel Interop の基本的な使い方は既にご存知の前提で書きます。
罫線のプロパティ値と実際の見た目が(私が英語に弱いので)頭の中で一致しなかったので一覧を作りました。
Excelの[セルの書式設定]、[罫線]に表示される罫線をコードで書いています。
留意点として、Microsoft の Learn には、LineStyle または Weight のどちらか一方を指定できるが、両方を同時に指定することはできない旨が記載されていました。
https://learn.microsoft.com/ja-jp/dotnet/api/microsoft.office.interop.excel.range.borderaround?view=excel-pia
以下で紹介する内容には、LineStyle Weight の両方を指定して実現できた罫線がありますが、Learn の記載と違うため、私の環境では可能でしたが、どんな環境でも再現可能かまでは確認していません。
もし、再現不可ケースがあればコメントいただけると嬉しいです。
サンプルコード
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook book = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
book = excel.Workbooks.Open(@"D:\罫線サンプル.xlsx");
sheet = (Worksheet)book.Worksheets[1];
// 1
Range targetRange = sheet.Range["B3", "D3"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlHairline;
// 2
targetRange = sheet.Range["B4", "D4"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDot;
// 3
targetRange = sheet.Range["B5", "D5"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDashDotDot;
// 4
targetRange = sheet.Range["B6", "D6"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDashDot;
// 5
targetRange = sheet.Range["B7", "D7"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDash;
// 6
targetRange = sheet.Range["B8", "D8"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThin;
// 7
targetRange = sheet.Range["F2", "H2"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDashDotDot;
targetRange.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlMedium;
// 8
targetRange = sheet.Range["F3", "H3"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlSlantDashDot;
// 9
targetRange = sheet.Range["F4", "H4"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDashDot;
targetRange.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlMedium;
// 10
targetRange = sheet.Range["F5", "H5"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDash;
targetRange.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlMedium;
// 11
targetRange = sheet.Range["F6", "H6"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlMedium;
// 12
targetRange = sheet.Range["F7", "H7"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;
// 13
targetRange = sheet.Range["F8", "H8"];
targetRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDouble;
book.Save();
}
catch (Exception ex)
{
Console.WriteLine("エラーが発生しました: " + ex.Message);
}
finally
{
// COM オブジェクトの解放
if (sheet != null)
{
Marshal.ReleaseComObject(sheet);
}
if (book != null)
{
book.Close(false);
Marshal.ReleaseComObject(book);
}
if (excel != null)
{
excel.Quit();
Marshal.ReleaseComObject(excel);
}
}
}