美文网首页.NET
EPPlus 和NPOI简单的Demo对比

EPPlus 和NPOI简单的Demo对比

作者: 张中华 | 来源:发表于2019-05-14 08:04 被阅读19次

    在之前的文章中简单的使用了下NPOI,NPOI的基本使用, 相对来说,并没有EPPLus好用。
    首先, EPPlus只用一个DLL,而不像NPOI引入多个dll,区分excel版本。
    其次,EPPlus在为某个cell赋值时,不要先创建cell,这也方便了使用。而且NPOI判定cell是不是new,并不是通过里面是否有值而判定的。
    最后,EPPlus更倾向面向对象编程。
    以上仅个人观点,欢迎指正。

    下面做了一个简单的Demo,分别使用EPPlus和NPOI插入一条数据,EPPlus用时272毫秒,NPOI用时499毫秒。
    所以,就该Demo来看,少量数据EPPlus的性能也是略胜一筹的。

    之后为了证明NPOI是否在处理大数据量的时候更胜一筹,又做了插入10000条数据的实验。第一次插入时, 确实NPOI在写入数据,有明显优势,可是后来更新数据时,很明显NPOI在读取数据又被拉开一大截。

    所以在初始化文件很小时,后来一次性需要插入大量数据数据时,使用NPOI极佳。

    以下是一个简单EPPlus的Demo:
    Code:

    using OfficeOpenXml;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ExcelHelper
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                string path = @"E:\C#每日一个知识点\TestFile\File.xlsx";
                FileInfo file = new FileInfo(path);
                using (var package = new ExcelPackage(file))
                {
                    var sheet = package.Workbook.Worksheets.Where(x => x.Name == "hello").First();
                    sheet.SetValue(1,1, "EPPlus value");
                    package.Save();
                }
    
                watch.Stop();
                Console.WriteLine($"Time is {watch.ElapsedMilliseconds} ms");
                Console.ReadLine();
            }
        }
    }
    
    

    那接下来,看一下NPOI的Demo:
    Code:

    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using NPOI.XSSF.UserModel;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace NPOIDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                string path = @"E:\C#每日一个知识点\TestFile\File.xlsx";
                IWorkbook workbook = null;
                using (var fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
                {
                    if (path.IndexOf(".xlsx") > 0) // 2007
                        workbook = new XSSFWorkbook(fs);
                    else if (path.IndexOf(".xls") > 0) // 2003
                        workbook = new HSSFWorkbook(fs);
                }
    
                var sheet = workbook.GetSheet("hello");
                if (sheet.GetRow(1) == null) sheet.CreateRow(1);
                if (sheet.GetRow(1).GetCell(1) == null) sheet.GetRow(1).CreateCell(1);
                sheet.GetRow(1).GetCell(1).SetCellValue("NPOI value");
    
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
                {
                    workbook.Write(fs);
                }
    
                watch.Stop();
                Console.WriteLine($"Time is {watch.ElapsedMilliseconds} ms");
                Console.ReadLine();
            }
        }
    }
    

    所以,就针对只插入一条数据来看,EPPlus相对用时较少。

    可是在网上看到的是,NPOI性能较好一些,那么再做一个Demo, 各操作一个File,分别插入10000条数据,分别查看用时和文件大小。


    EPPlus第一次执行 EPPlus第N次执行
    NPOI第一次执行
    NPOI第N次执行

    相关文章

      网友评论

        本文标题:EPPlus 和NPOI简单的Demo对比

        本文链接:https://www.haomeiwen.com/subject/xdmdaqtx.html