epplus开始使用epplus


备注

EPPlus是一个.NET库,使用Open Office Xml格式(xlsx)读取和写入Excel 2007/2010/2013文件。

EPPlus支持:

  • 细胞范围
  • 细胞样式(边框,颜色,填充,字体,数字,对齐)
  • 图表
  • 图片
  • 形状
  • 评论
  • 保护
  • 加密
  • 数据透视表
  • 数据验证
  • 条件格式
  • VBA
  • 公式计算

版本

发布日期
首次发布 2009-11-30
2.5.0.1 2010-01-25
2.6.0.1 2010-03-23
2.7.0.1 2010-06-17
2.8.0.2 2010-11-15
2.9.0.1 2011-05-31
3.0.0.2 2012-01-31
3.1 2012-04-11
4.0.5 2016年1月8日
4.1 2016年7月14日

入门

//Create a new ExcelPackage
using (ExcelPackage excelPackage = new ExcelPackage())
{
   //Set some properties of the Excel document
   excelPackage.Workbook.Properties.Author = "VDWWD";
   excelPackage.Workbook.Properties.Title = "Title of Document";
   excelPackage.Workbook.Properties.Subject = "EPPlus demo export data";
   excelPackage.Workbook.Properties.Created = DateTime.Now;

    //Create the WorkSheet
    ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");

    //Add some text to cell A1
    worksheet.Cells["A1"].Value = "My first EPPlus spreadsheet!";
    //You could also use [line, column] notation:
    worksheet.Cells[1,2].Value = "This is cell B1!";

    //Save your file
    FileInfo fi = new FileInfo(@"Path\To\Your\File.xlsx");
    excelPackage.SaveAs(fi);
}

//Opening an existing Excel file
FileInfo fi = new FileInfo(@"Path\To\Your\File.xlsx");
using (ExcelPackage excelPackage = new ExcelPackage(fi))
{
    //Get a WorkSheet by index. Note that EPPlus indexes are base 1, not base 0!
    ExcelWorksheet firstWorksheet = excelPackage.Workbook.Worksheets[1];
    
    //Get a WorkSheet by name. If the worksheet doesn't exist, throw an exeption
    ExcelWorksheet namedWorksheet = excelPackage.Workbook.Worksheets["SomeWorksheet"];

    //If you don't know if a worksheet exists, you could use LINQ,
    //So it doesn't throw an exception, but return null in case it doesn't find it
    ExcelWorksheet anotherWorksheet = 
        excelPackage.Workbook.Worksheets.FirstOrDefault(x=>x.Name=="SomeWorksheet");

    //Get the content from cells A1 and B1 as string, in two different notations
    string valA1 = firstWorksheet.Cells["A1"].Value.ToString();
    string valB1 = firstWorksheet.Cells[1,2].Value.ToString();

    //Save your file
    excelPackage.Save();
}
 

安装

CodePlex下载文件并将其添加到项目中。

或者使用程序包管理器安装文件。

PM> Install-Package EPPlus