Skip to content

Instantly share code, notes, and snippets.

@Pie001
Last active December 21, 2016 12:23
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 Pie001/f8e80a22c9487acf3436151126367616 to your computer and use it in GitHub Desktop.
Save Pie001/f8e80a22c9487acf3436151126367616 to your computer and use it in GitHub Desktop.
EPPLUS テンプレートでエクセルファイル作成(Stream)
using System.IO;
using OfficeOpenXml;
// テンプレートファイルを元にDBから取得した値を指定したセルに設定したい場合、且つbyte[]をエクセルでファイルダウンロードさせる場合
byte[] source;
MemoryStream stream = new MemoryStream();
using (var template = System.IO.File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/template.xlsx"))
using (var package = new ExcelPackage(stream, template))
{
ExcelWorksheet worksheet = null;
worksheet = package.Workbook.Worksheets.Where(s => s.Name == "Sheet1").FirstOrDefault();
// 設定したい内容を設定
worksheet.Cells[5, 2].Value = "value1";
worksheet.Cells[5, 3].Value = "value2";
worksheet.Cells[5, 4].Value = "value3";
package.Save();
source = stream.ToArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment