• 使用ICSharpCode.SharpZipLib+Aspose模板批量导出Word


    由于是Web端的项目,所以点击按钮之后直接从Aspose模板读取数据,然后在内存中操作,而不是下载到本地后再打包弄到内存中下载。废话不多说,直接上代码

     1      public ActionResult ExportZip(int testid)
     2         {
     3             string strSavePath = Server.MapPath("~/WordTemplate/PersonalityTest.zip");
     4             //获取数据,用户可以根据自己的需求重新定义
     5             var Tester = testerBll.GetAllList();
     6             string zipFileName = "人力资源性格测试.zip";
     7             MemoryStream ms = new MemoryStream();
     8             byte[] buffer = null;
     9             var filePath = string.Empty;
    10             using (ZipFile file = ZipFile.Create(ms))
    11             {
    12                 file.BeginUpdate();
    13                 //此处的循环用户可以根据自己的需求自己定义
    14                 foreach (var t in Tester)
    15                 {
    16                     var data = new Dictionary<string, int>();
    17                     #region 生成word文件并填充数据
    18                     string templatePath = Server.MapPath("~/WordTemplate/Test.doc");
    19                     var doc = new Document(templatePath); // 载入模板
    20 
    21                     // 填充数据
    22                     doc.Range.Bookmarks["A"].Text = data.ContainsKey("A") ? data["A"].ToString() : string.Empty;
    23                     doc.Range.Bookmarks["B"].Text = data.ContainsKey("B") ? data["B"].ToString() : string.Empty;
    24                     doc.Range.Bookmarks["C"].Text = data.ContainsKey("C") ? data["C"].ToString() : string.Empty;
    25                     doc.Range.Bookmarks["D"].Text = data.ContainsKey("D") ? data["D"].ToString() : string.Empty;
    26                     doc.Range.Bookmarks["E"].Text = data.ContainsKey("E") ? data["E"].ToString() : string.Empty;
    27                     doc.Range.Bookmarks["F"].Text = data.ContainsKey("F") ? data["F"].ToString() : string.Empty;
    28                     doc.Range.Bookmarks["G"].Text = data.ContainsKey("G") ? data["G"].ToString() : string.Empty;
    29                     doc.Range.Bookmarks["H"].Text = data.ContainsKey("H") ? data["H"].ToString() : string.Empty;
    30                     doc.Range.Bookmarks["I"].Text = data.ContainsKey("I") ? data["I"].ToString() : string.Empty;
    31                     doc.Range.Bookmarks["count"].Text = count.ToString();
    32                     doc.Range.Bookmarks["name"].Text = tester.name;
    33                     doc.Range.Bookmarks["tel"].Text = tester.tel;
    34                     //doc.Range.Bookmarks["result"].Text = result.Caption.Replace("<br/>", string.Empty); // 剔除 <br/> 换行符号
    35                     doc.Range.Bookmarks["result"].Text = string.IsNullOrEmpty(result.Caption) ? string.Empty : result.Caption.Replace("<br/>", string.Empty); // 剔除 <br/> 换行符号
    36                     #endregion
    37                     // 受测人姓名.doc
    38                     var fileName = string.Format("{0}.doc", t.name);
    39                     using (MemoryStream stream = new MemoryStream())
    40                     {
    41                         doc.Save(stream, SaveFormat.Doc);
    42                         StringDataSource ds = new StringDataSource(stream);
    43                         file.Add(ds, fileName);
    44                     }
    45                     
    46                 }
    47                 file.CommitUpdate();
    48                 buffer = new byte[ms.Length];
    49                 ms.Position = 0;
    50                 ms.Read(buffer, 0, buffer.Length);   //读取文件内容(1次读ms.Length/1024M)  
    51                 ms.Flush();
    52                 ms.Close();
    53             }
    54             Response.Clear();
    55             Response.Buffer = true;
    56             Response.ContentType = "application/x-zip-compressed";
    57             Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName));
    58             Response.BinaryWrite(buffer);
    59             Response.Flush();
    60             Response.End();
    61 
    62             return View();
    63         }

    还有一个实现了IStaticDataSource接口的内部类

     1     class StringDataSource : IStaticDataSource
     2     {
     3         public byte[] bytes { get; set; }
     4         public StringDataSource(MemoryStream ms)
     5         {
     6             bytes = ms.GetBuffer();
     7         }
     8 
     9         public Stream GetSource()
    10         {
    11             Stream s = new MemoryStream(bytes);
    12             return s;
    13         }
    14     }

    为了防止生成的压缩文件乱码,可以在using之上加上这两句代码

    1 Encoding gbk = Encoding.GetEncoding("gbk");
    2 ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage;
  • 相关阅读:
    禅道admin忘记密码
    redis conf 解析
    MySQL 安装
    Centos7上安装docker (抄)
    Linux查看CPU和内存使用情况 抄
    上传图片到阿里云OSS和获取上传图片的外网url的步骤
    docker mysql
    Oracle 11g,exp导出时空表、少表的解决办法
    使用com.aspose.words将word模板转为PDF乱码解决方案
    oracle数据库,检索出某几个字段不唯一的那些数据
  • 原文地址:https://www.cnblogs.com/wangyulong/p/6592624.html
Copyright © 2020-2023  润新知