Last active
June 25, 2021 00:44
-
-
Save devlights/10cad2795e5601c1fc45 to your computer and use it in GitHub Desktop.
EPPlusの基本的な使い方メモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
// For EPPlus | |
using OfficeOpenXml; | |
using OfficeOpenXml.Style; | |
namespace TryEPPlus | |
{ | |
/// <summary> | |
/// ファイル操作のためのユーティリティ | |
/// </summary> | |
public static class FileInfoExtensions | |
{ | |
public static FileInfo DeleteIfExists(this FileInfo self) | |
{ | |
if (self.Exists) | |
{ | |
self.Delete(); | |
} | |
return self; | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
var obj = new Program(); | |
obj.BasicExcelOperations(); | |
obj.BasicExcelStyles(); | |
Process.Start(Environment.CurrentDirectory); | |
} | |
private void BasicExcelOperations() | |
{ | |
// | |
// EPPlusはOpen XMLの仕様を元にExcelのデータ操作を行うライブラリ | |
// | |
// 公式ページ | |
// http://epplus.codeplex.com/ | |
// | |
// Open XMLデータを直接内部で作成してくれるのでExcelのインストールが必要ない。 | |
// その代わりに、Excel 2007より前のバージョンのExcelファイル(つまり、拡張子がxlsのもの) | |
// は扱えない。 | |
// | |
// インストールは、nugetから行える | |
// $ Install-Package EPPlus | |
// | |
// 使用する場合、以下の名前空間を参照するようにする | |
// using OfficeOpenXml; | |
// using OfficeOpenXml.Style; | |
// | |
// また、フォントの設定などを行う場合、EPPlusはSystem.Drawingの方の | |
// Fontクラスなどを要求するため、System.Drawing.dllなども参照設定しておく | |
// 方が良い。 | |
// | |
var outputFile = new FileInfo(@"excel1.xlsx").DeleteIfExists(); | |
// | |
// 最初にExcelPackageを作成する。 | |
// IDisposableを実装しているのでusing可能。 | |
// | |
using (var excel = new ExcelPackage(outputFile)) | |
{ | |
// | |
// シート追加 | |
// | |
var sheet = excel.Workbook.Worksheets.Add("テストシート"); | |
// | |
// セル取得 | |
// | |
var cell = sheet.Cells[1, 1]; | |
// | |
// セルに値設定 | |
// | |
cell.Value = "こんにちは世界"; | |
// | |
// そのままだとフォントが英語圏のフォントなので調整 | |
// | |
cell.Style.Font.SetFromFont(new Font("MS Gothic", 10, FontStyle.Regular)); | |
// | |
// 保存 | |
// | |
excel.Save(); | |
// | |
// EPPlusは、Open XMLデータを直接作っているため | |
// Interopを利用してExcel操作をする場合と違い、参照カウンタの心配を | |
// する必要がない。 | |
// | |
// Interop経由でのExcelの操作については | |
// http://jeanne.wankuma.com/tips/vb.net/programming/releasecom.html | |
// http://devlights.hatenablog.com/entry/20100223/p1 | |
// などを参照ください。 | |
// | |
} | |
} | |
private void BasicExcelStyles() | |
{ | |
using (var excel = new ExcelPackage(new FileInfo("excel2.xlsx").DeleteIfExists())) | |
{ | |
// | |
// セルのスタイルの設定まわり | |
// | |
var sheet = excel.Workbook.Worksheets.Add("test sheet"); | |
var cell = sheet.Cells["A1"]; | |
cell.Value = "hello world"; | |
// | |
// フォント設定 | |
// | |
var style = cell.Style; | |
style.Font.SetFromFont(new Font("MS Gothic", 10L, FontStyle.Bold)); | |
// | |
// 水平位置 | |
// | |
style.HorizontalAlignment = ExcelHorizontalAlignment.Right; | |
// | |
// 垂直位置 | |
// | |
style.VerticalAlignment = ExcelVerticalAlignment.Bottom; | |
// | |
// コメント | |
// | |
var comment = cell.AddComment("コメントも作れます.", "EPPlus"); | |
comment.Visible = true; | |
// | |
// 背景色 | |
// | |
style.Fill.PatternType = ExcelFillStyle.Solid; | |
style.Fill.BackgroundColor.SetColor(Color.Red); | |
// | |
// 前景色 | |
// | |
style.Font.Color.SetColor(Color.Blue); | |
// | |
// 罫線 | |
// | |
excel.Save(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment