EPplus的读写

  1. POI(http://npoi.codeplex.com/)
  2. MyXls(http://sourceforge.net/projects/myxls/)
  3. Koogra(http://sourceforge.net/projects/koogra/)
  4. ExcelLibrary(http://code.google.com/p/excellibrary/)
  5. ExcelPackage(http://excelpackage.codeplex.com/)
  6. EPPlus(http://epplus.codeplex.com/)
  7. LinqToExcel(http://code.google.com/p/linqtoexcel/)

其中大部分的意见都认为“对于Excel 97-2003格式,还是用NPOI最好;而对于2007(xlsx)以上版本,可以使用EPPlus”。由于工作中基本上都是使用xlsx,因此这里直接选择了EPPlus。

EPPlus读取excel:

复制代码
using (ExcelPackage package = new ExcelPackage(new FileStream(path, FileMode.Open)))
{
    for (int i = 1; i <= package.Workbook.Worksheets.Count; ++i)
    {
        ExcelWorksheet sheet = package.Workbook.Worksheets[i];
        for (int j = sheet.Dimension.Start.Column, k = sheet.Dimension.End.Column; j <= k; j++)
        {
            for (int m = sheet.Dimension.Start.Row, n = sheet.Dimension.End.Row; m <= n; m++)
            {
                string str = GetValue(sheet, m, j);
                if (str != null)
                {
                    // do something
                }
            }
        }
    }
}
复制代码

EPPlus写入excel:

复制代码
using (ExcelPackage package = new ExcelPackage())
{
    ExcelWorksheet sheet = package.Workbook.Worksheets.Add("Sheet1");
    sheet.Cells[1, 1].Value = "1";
    sheet.Cells[1, 2].Value = "2";
    sheet.Cells[1, 3].Value = "3";
    sheet.Cells[1, 4].Value = "4";
    sheet.Cells[1, 5].Value = "5";
    sheet.Cells[1, 6].Value = "6";
    using (Stream stream = new FileStream(path, FileMode.Create))
    {
        package.SaveAs(stream);
    }
}
复制代码

原文地址:https://www.cnblogs.com/libla/p/5824296.html

原文地址:https://www.cnblogs.com/LinWenQiang/p/13917811.html