Created
October 29, 2014 02:11
-
-
Save ishisaka/20208597a18146199846 to your computer and use it in GitHub Desktop.
EPplusを使ったExcel Hello World
This file contains hidden or 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.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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EPplus http://epplus.codeplex.com/