0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C# Interop Excel】罫線の見た目サンプル一覧

Last updated at Posted at 2025-02-23

※本文は 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);
        }
    }
}

出力結果
罫線サンプル.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?