zoukankan      html  css  js  c++  java
  • 大数据插入Excel报错处理

    发现问题:

    最近运行程序时,发现了一个问题,就是在导出excel时,报了一下错误

    image

    分析问题:

    原来是由于NPOI这个动态库导致的,然后看了下版本,发现是1.2.5。然后百度了下,发现这个版本的NPOI只支持office2003,二office2003最多支持65536行,找到问题,下面就开始处理问题

    解决问题:

    从NPOI考虑:继续用这个版本的动态库,只是在插入数据的时候,加个判断,如果数据条数大于65536时,就在创建一个sheet

    从office考虑:考虑使用跟高的Office,发现NPOI的2.1.3支持更高版本的office

    我的处理方式:

    方式一:不换动态库,还是老古董

    static void Main(string[] args)
    {
        IWorkbook wk=new HSSFWorkbook();
        ISheet sheet = wk.CreateSheet("StudentK");
        ISheet sheet2 = wk.CreateSheet("TeacherL");
            
        Stopwatch sw =new Stopwatch();
        sw.Start();
        for (int i = 0; i < 75535; i++)
        {
            if (i<=65535)
            {
                IRow row = sheet.CreateRow(i);
                row.CreateCell(0).SetCellValue("Kimisme");
                row.CreateCell(1).SetCellValue(i.ToString());
                row.CreateCell(2).SetCellValue(DateTime.Now.ToString("s"));
            }
            else if (i > 65535)
            {
                IRow row = sheet2.CreateRow(i-65536);
                row.CreateCell(0).SetCellValue("Kimisme");
                row.CreateCell(1).SetCellValue(i.ToString());
                row.CreateCell(2).SetCellValue(DateTime.Now.ToString("s"));
            }
        }
        sw.Stop();
        Console.WriteLine(sw.ElapsedMilliseconds);
        sw.Restart();
        using (FileStream writer =File.OpenWrite("老古董.xls"))
        {
            wk.Write(writer);
        }
        Console.WriteLine(sw.ElapsedMilliseconds);
        Console.WriteLine("ok");
        Console.ReadKey();
    }

    成果图

    image

    方式二:我就用新东西

    static void Main(string[] args)
    {
        //改动的地方有两处,这是第一处
        IWorkbook wk = new XSSFWorkbook();
        ISheet sheet = wk.CreateSheet("FrientS");
        
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < 75535; i++)
        {
          IRow row = sheet.CreateRow(i);
                row.CreateCell(0).SetCellValue("Kimisme");
                row.CreateCell(1).SetCellValue(i.ToString());
                row.CreateCell(2).SetCellValue(DateTime.Now.ToString("s"));
        }
        sw.Stop();
        Console.WriteLine(sw.ElapsedMilliseconds);
        sw.Restart();
        //这是第二处
        using (FileStream writer = File.OpenWrite("新事物.xlsx"))
        {
            wk.Write(writer);
        }
        Console.WriteLine(sw.ElapsedMilliseconds);
        Console.WriteLine("ok");
        Console.ReadKey();
    }

    成果图:

    image

    最后,感谢面向对象,感谢里氏替换原则,理由如下

    • HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
    • XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
    更多精彩内容请看:http://www.cnblogs.com/2star
  • 相关阅读:
    同步请求和异步请求的区别
    Ajax初步理解
    ajax的GET和POST请求
    What's this?(js)
    rxjs
    Angular7_获取异步方法里面的数据
    Angular7_人员登记系统
    Angular7
    特殊操作符
    Oracle 表操作
  • 原文地址:https://www.cnblogs.com/kimisme/p/5111248.html
Copyright © 2011-2022 走看看