
C#からEXECELの罫線の引き方を紹介していきます。
EXCEL操作をするときによく使うので是非参考にしてみてください。
他にもEXCEL操作関係の記事を記載していますので、参考にしてみてください。
C# EXCEL操作 新規ブック作成
C# EXCEL操作 値の入力
C# EXCEL操作 背景色変更
C# EXCEL操作 フィルター設定
罫線 外枠の引き方
それでは使い方を見ていきましょう。
Excel.Range range_line;
range_line = sheet.Range[sheet.Cells[2, 2], sheet.Cells[4, 4]];
Excel.Borders borders;
Excel.Border border;
borders = range_line.Borders;
border = borders[Excel.XlBordersIndex.xlEdgeLeft];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border = borders[Excel.XlBordersIndex.xlEdgeRight];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border = borders[Excel.XlBordersIndex.xlEdgeTop];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border = borders[Excel.XlBordersIndex.xlEdgeBottom];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
解説
1~2行目:エクセルの範囲を指定
4~5行目:罫線を引くための型を宣言
7~15行目:罫線の設定
8行目:指定した範囲の左端に線を引く
9行目:LineStyleの設定 Excel.XlLineStyle.xlContinuous 普通の線を指定
10行目:指定した範囲の右端に線を引く
12行目:指定した範囲の上端に線を引く
14行目:指定した範囲の下端に線を引く
結果
指定した範囲(2,2)~(4,4)の左、右、上、下に罫線が引かれていることを確認できました。
罫線 明細の引き方
先ほど外枠を作成したので、続いて明細に罫線を引いてみましょう。
border = borders[Excel.XlBordersIndex.xlInsideVertical];
border.LineStyle = Excel.XlLineStyle.xlDot;
border = borders[Excel.XlBordersIndex.xlInsideHorizontal];
border.LineStyle = Excel.XlLineStyle.xlDot;
解説
1行目:指定した範囲の内側のすべての縦線
2行目:点線
3行目:指定した範囲の内側のすべての横線
4行目:点線
結果
先ほど作成した外枠に加えて、明細に点線を引くことができましたね。
サンプルソース
サンプルソースを載せておきます。
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace c_sharp
{
public partial class frmExcel : Form
{
public frmExcel()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbooks excelBooks = excelApp.Workbooks;
Excel.Workbook excelBook = excelBooks.Add();
Excel.Worksheet sheet = excelApp.Worksheets["sheet1"];
try
{
excelApp.Visible = false;
Excel.Range range_line;
range_line = sheet.Range[sheet.Cells[2, 2], sheet.Cells[4, 4]];
Excel.Borders borders;
Excel.Border border;
borders = range_line.Borders;
border = borders[Excel.XlBordersIndex.xlEdgeLeft];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border = borders[Excel.XlBordersIndex.xlEdgeRight];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border = borders[Excel.XlBordersIndex.xlEdgeTop];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border = borders[Excel.XlBordersIndex.xlEdgeBottom];
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border = borders[Excel.XlBordersIndex.xlInsideVertical];
border.LineStyle = Excel.XlLineStyle.xlDot;
border = borders[Excel.XlBordersIndex.xlInsideHorizontal];
border.LineStyle = Excel.XlLineStyle.xlDot;
excelBook.SaveAs("D:\\NewExcelBook_csharp.xlsx");
}
catch
{
throw;
}
finally
{
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
}
}
}
}
結果
まとめ
EXCELの罫線の引き方を紹介しました。
実践や点線以外にも様々な線を指定することができるので是非試してみてください。
~罫線の引く場所~
Excel.XlBordersIndex.xlEdgeLeft //範囲の左側
Excel.XlBordersIndex.xlEdgeRight //範囲の右側
Excel.XlBordersIndex.xlEdgeTop //範囲の上側
Excel.XlBordersIndex.xlEdgeBottom //範囲の下側
Excel.XlBordersIndex.xlInsideVertical //範囲の内側の縦線
Excel.XlBordersIndex.xlInsideHorizontal //範囲の内側の横線
Excel.XlBordersIndex.xlDiagonalDown //斜めの線 左上から右下
Excel.XlBordersIndex.xlDiagonalUp //斜めの線 左下から右上
~罫線の種類~
Excel.XlLineStyle.xlContinuous
Excel.XlLineStyle.xlDash
Excel.XlLineStyle.xlDashDot
Excel.XlLineStyle.xlDashDotDot
Excel.XlLineStyle.xlDot
Excel.XlLineStyle.xlDouble
Excel.XlLineStyle.xlLineStyleNone
Excel.XlLineStyle.xlSlantDashDot