zoukankan      html  css  js  c++  java
  • ICSharpCode.SharpZipLib.Zip.ZipException: EOF in header

    //异常提示:ICSharpCode.SharpZipLib.Zip.ZipException: EOF in header 
    
    NPOIMemoryStream stream = new NPOIMemoryStream();
    using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite))
    {
        fileStream.CopyTo(stream);
        stream.Position = 0; //这一句不加就会上面的异常错误
    }
    IWorkbook wk = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);

     还有种情况报这个错误,NPOI打开了空的流对象,这种情况NPOI不用传流对象创建IWorkbook对象,直接调用无参构造函数,最后操作完之后将结果写入到流中;最后提示一下:写入流之前也需要 stream.Position = 0;

    protected IWorkbook GetWorkbook(string ext)
    {
        IWorkbook book = null;
        if (ext.ToLower().Equals(".xls"))
        {
            book = new HSSFWorkbook();
        }
        else if (ext.ToLower().Equals(".xlsx"))
        {
            book = new NPOI.XSSF.UserModel.XSSFWorkbook();
        }
        else
        {
            throw new SinoException("只支持Excel文件");
        }
        return book;
    }

    打开Excel文件流的方式

    protected IWorkbook OpenWorkbook(Stream stream, string ext)
    {
        IWorkbook book = null;
        if (ext.ToLower().Equals(".xls"))
        {
            book = new HSSFWorkbook(stream);
        }
        else if (ext.ToLower().Equals(".xlsx"))
        {
            book = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);
        }
        else
        {
            throw new SinoException("只支持Excel文件");
        }
        return book;
    }
      public class NPOIMemoryStream : MemoryStream
      {
            /// <summary>
            /// 获取流是否关闭
            /// </summary>
            public bool IsColse
            {
                get;
                private set;
            }
            public NPOIMemoryStream(bool colse = false)
            {
                IsColse = colse;
            }
            public override void Close()
            {
                if (IsColse)
                {
                    base.Close();
                }
            }
      }
  • 相关阅读:
    使用密码解密TACACS+的报文
    C9K Stackwise Virtual(三)
    Webhook Configuration Example
    sup-bootflash和bootflash
    WLC5508 license没有500个?
    AAA Server Groups
    关于FlexConnect的Bug!
    Bug搬运工-CSCve57121--Cisco 2800, 3800 and 1560 series APs fail to pass traffic
    Bug搬运工-CSCvb29354-1810 OEAP cannot join vWLC
    阿里云云计算认证ACP模拟考试练习题第1套模拟题分享(共10套)
  • 原文地址:https://www.cnblogs.com/yiyanwei/p/14196664.html
Copyright © 2011-2022 走看看