加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – NPOI – 从模板文件创建的保存工作簿

发布时间:2020-12-16 03:58:52 所属栏目:asp.Net 来源:网络整理
导读:这是我第二次与NPOI打交道.我正在从这样的模板excel文件创建工作簿. string dldir = AppDomain.CurrentDomain.BaseDirectory + "Downloadexcel"; string uldir = AppDomain.CurrentDomain.BaseDirectory + "Uploaddefault_export_file"; string file
这是我第二次与NPOI打交道.我正在从这样的模板excel文件创建工作簿.

string dldir = AppDomain.CurrentDomain.BaseDirectory + "Downloadexcel";
        string uldir = AppDomain.CurrentDomain.BaseDirectory + "Uploaddefault_export_file";
        string filename = DateTime.Now.ToString("yyyy-M-d") + "_" + user.first_name + "_" + query.default_export_filename;

        System.IO.File.Delete(Path.Combine(dldir,filename));

        //System.IO.File.Copy(Path.Combine(uldir,query.default_export_filename),Path.Combine(dldir,filename));


        HSSFWorkbook hssfwb;
        using (FileStream file = new FileStream(Path.Combine(uldir,FileMode.Open,FileAccess.ReadWrite))
        {
            hssfwb = new HSSFWorkbook(file);
        }

        MemoryStream mstream = new MemoryStream();
        hssfwb.Write(mstream);

        FileStream xfile = new FileStream(Path.Combine(dldir,filename),FileMode.Create,System.IO.FileAccess.Write);
        byte[] bytes = new byte[mstream.Length];
        mstream.Read(bytes,(int)mstream.Length);
        xfile.Write(bytes,bytes.Length);
        xfile.Close();
        mstream.Close();

但是当我检查创建的文件时.它什么都没有.大小是正确的.但内容只是空白.我也无法在MS Excel中打开它.我重复并跟踪代码,但我似乎无法找到导致它只写空白的原因.

任何人都知道为什么会这样

仅供参考:我正在使用从NuGet Gallery安装的VS 2010,ASP.Net MVC 4和NPOI 1.2.5

解决方法

这个怎么样:

MemoryStream mstream = new MemoryStream();
hssfwb.Write(mstream);

FileStream xfile = new FileStream(Path.Combine(dldir,System.IO.FileAccess.Write);
byte[] bytes = new byte[mstream.Length];
mstream.Read(bytes,(int)mstream.Length);
xfile.Write(bytes,bytes.Length);
xfile.Close();
mstream.Close();

改性:

FileStream xfile = new FileStream(Path.Combine(dldir,System.IO.FileAccess.Write);
hssfwb.Write(xfile);
xfile.Close();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读