2. 2
Outline
• Introduction EPPlus
• Other Library
• Limit & License
• How to use
• Snippet Code
• References
3. 3
Introduction EPPLus
• EPPlus is a .net library that reads and writes
Excel 2007/2010 files using the Open Office Xml
format (xlsx).
• EPPlus 提供簡單、便捷的 API 介面
▫ 用 .Cells[rowIndex, colIndex] 就能直接存取欄位
▫ 用 .Cells[r1:c1] 就能取得一段選取範圍
▫ 用 Cells[…].Style.Font.Color.SetColor(Color.Red)
改變字型
▫ Many more
4. 4
Other Library
• NPOI Library
▫ 源自於 Apache POI 專案
• Open XML SDK
▫ 微軟提供的函式庫
• OpenOffice.org SDK
▫ OpenOffice
• LinqToExcel
▫ 只限於讀取
5. 5
Limit & License
• Only Support Excel 2007/2010
• LGPL ( GNU Lesser General Public
License )
▫ 純引用 Library 不用 open source.
▫ 有修改 Library, 修改的部份要及產品中使用到的
Source Code 要 Open Source
6. 6
How to use
• Download
▫ http://epplus.codeplex.com/
▫ NuGet
• Reference EPPlus.dll in your project
7. 7
Snippet Code
//Create Excel
FileInfo newFile = new FileInfo(outputDir.FullName +
@"sample6.xlsx");
ExcelPackage pck = new ExcelPackage(newFile);
//Add the Content sheet
var ws = pck.Workbook.Worksheets.Add("Content");
//set style
ws.Cells["B1:E1"].Style.Font.Bold = true;
//set value
ws.Cells["B1"].Value = "Name";
8. 8
EPPlus and Linq
var sheet1 = pck.Workbook.Worksheets["Sheet1"];
var query = (from cell in sheet1.Cells["a:a"]
where cell.Value != ""
select cell);
query3.SingleOrDefault().Value.Dump();
Version 2.8 has added support for
enumeration of cells....
9. 9
NPOI V.S EPPlus
NPOI EPPLUs
// 建立 worksheet var pck = new ExcelPackage(new
var workBook = new HSSFWorkbook(); FileInfo(@"D:EPPLus.xlsx"));
// 建立 sheet // 建立 sheet
var sheet1 = var sheet1 =
workBook.CreateSheet("Sheet1");
pck.Workbook.Worksheets.Add("Sheet1")
;
// 設值
sheet1.CreateRow(1).CreateCell(1).SetCell
Value("XXX"); // 設值
sheet1.Cells[1, 1].Value = "XXX";
// 存檔
FileStream file = new // 存檔
FileStream(@"C:NPOI.xls", pck.Save();
FileMode.Create);
hssfworkbook.Write(file);
file.Close();
10. 10
References
• 比NPOI更討喜的Excel元件-EPPlus!
• EPPlus-Create advanced Excel 2007 spreadsheets on
• [C#] NPOI、OpenXML SDK、
OpenOffice.org SDK 寫入資料到 EXCEL 檔案
• 在 Server 端存取 Excel 檔案的利器:
NPOI Library
• Creating Reports in Excel 2007 using EPPlus (