Skip to content

Instantly share code, notes, and snippets.

@ishisaka
Created October 29, 2014 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishisaka/20208597a18146199846 to your computer and use it in GitHub Desktop.
Save ishisaka/20208597a18146199846 to your computer and use it in GitHub Desktop.
EPplusを使ったExcel Hello World
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace EPplusSample1
{
/// <summary>
/// EPPlusを使ったExcel操作のサンプル。
/// </summary>
class Program
{
/// <summary>
/// エントリーポイント
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
var newFile = new FileInfo(@"sample1.xlsx");
using (var package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = null;
// Excelファイルが既に存在するなら、該当ワークシートが存在するか確認する。
if (newFile.Exists)
{
worksheet = package.Workbook.Worksheets.Where(s => s.Name == "Hello").FirstOrDefault();
}
// ワークシートが存在しなければ、ワークシートを追加する
if (worksheet == null)
{
worksheet = package.Workbook.Worksheets.Add("Hello");
}
// セルに文字列を挿入する。
worksheet.Cells[1, 1].Value = "こんにちは、世界。";
//フォントの色を設定。
worksheet.Cells[1, 1].Style.Font.Color.SetColor(Color.Red);
//Excelのファイルをセーブする。
package.Save();
}
}
}
}
@ishisaka
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment