0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

c# EPPlusの使い方

Posted at

EPPlus version 5以降は、有料になるので、
普通の開発は、EPPlus version 4で良いかと思います。

Excelファイル作成

class Program
{
    static void Main(string[] args)
    {
        // 出力ファイルの準備(実行ファイルと同じフォルダに出力される)
        FileInfo newFile = new FileInfo("result.xlsx");
        if (newFile.Exists)
        {
            newFile.Delete();
            newFile = new FileInfo("result.xlsx");
        }
 
        // Excelファイルの作成
        using (ExcelPackage package = new ExcelPackage(newFile))
        {
            // ワークシートを1枚追加
            ExcelWorksheet sheet = package.Workbook.Worksheets.Add("シート名");
 
            // A1セルに書き込み
            sheet.Cells["A1"].Value = "Hello World";
 
            // セルはR1C1形式でも指定可
            sheet.Cells[2, 1].Value = 27;
 
            // 保存
            package.Save();
        }
    }
}

枠線色設定

//枠線色をグレーにして、線を細くする
sheet.Cells[2, 1].Style.Border.BorderAround(ExcelBoderStyle.Thin,ColorTranslator.FromHtml("#7f7f7f"));

セル背景色設定

sheet.Cells[2, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
sheet.Cells[2, 1].Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#7f7f7f"));

文字列中央揃え

sheet.Cells[2,1].Style.HorizontalAlignment = ExcelHorizontalAlignment.CenterContinuous;

セル結合

sheet.Cells[2,1,2,4].Merge = true;

参考サイト

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?