使用ICSharpCode.SharpZipLib+Aspose模板批量导出Word
2017-03-21 08:11
148 查看
由于是Web端的项目,所以点击按钮之后直接从Aspose模板读取数据,然后在内存中操作,而不是下载到本地后再打包弄到内存中下载。废话不多说,直接上代码
还有一个实现了IStaticDataSource接口的内部类
为了防止生成的压缩文件乱码,可以在using之上加上这两句代码
public ActionResult ExportZip(int testid) { string strSavePath = Server.MapPath("~/WordTemplate/PersonalityTest.zip"); //获取数据,用户可以根据自己的需求重新定义 var Tester = testerBll.GetAllList(); string zipFileName = "人力资源性格测试.zip"; MemoryStream ms = new MemoryStream(); byte[] buffer = null; var filePath = string.Empty; using (ZipFile file = ZipFile.Create(ms)) { file.BeginUpdate(); //此处的循环用户可以根据自己的需求自己定义 foreach (var t in Tester) { var data = new Dictionary<string, int>(); #region 生成word文件并填充数据 string templatePath = Server.MapPath("~/WordTemplate/Test.doc"); var doc = new Document(templatePath); // 载入模板 // 填充数据 doc.Range.Bookmarks["A"].Text = data.ContainsKey("A") ? data["A"].ToString() : string.Empty; doc.Range.Bookmarks["B"].Text = data.ContainsKey("B") ? data["B"].ToString() : string.Empty; doc.Range.Bookmarks["C"].Text = data.ContainsKey("C") ? data["C"].ToString() : string.Empty; doc.Range.Bookmarks["D"].Text = data.ContainsKey("D") ? data["D"].ToString() : string.Empty; doc.Range.Bookmarks["E"].Text = data.ContainsKey("E") ? data["E"].ToString() : string.Empty; doc.Range.Bookmarks["F"].Text = data.ContainsKey("F") ? data["F"].ToString() : string.Empty; doc.Range.Bookmarks["G"].Text = data.ContainsKey("G") ? data["G"].ToString() : string.Empty; doc.Range.Bookmarks["H"].Text = data.ContainsKey("H") ? data["H"].ToString() : string.Empty; doc.Range.Bookmarks["I"].Text = data.ContainsKey("I") ? data["I"].ToString() : string.Empty; doc.Range.Bookmarks["count"].Text = count.ToString(); doc.Range.Bookmarks["name"].Text = tester.name; doc.Range.Bookmarks["tel"].Text = tester.tel; //doc.Range.Bookmarks["result"].Text = result.Caption.Replace("<br/>", string.Empty); // 剔除 <br/> 换行符号 doc.Range.Bookmarks["result"].Text = string.IsNullOrEmpty(result.Caption) ? string.Empty : result.Caption.Replace("<br/>", string.Empty); // 剔除 <br/> 换行符号 #endregion // 受测人姓名.doc var fileName = string.Format("{0}.doc", t.name); using (MemoryStream stream = new MemoryStream()) { doc.Save(stream, SaveFormat.Doc); StringDataSource ds = new StringDataSource(stream); file.Add(ds, fileName); } } file.CommitUpdate(); buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M) ms.Flush(); ms.Close(); } Response.Clear(); Response.Buffer = true; Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName)); Response.BinaryWrite(buffer); Response.Flush(); Response.End(); return View(); }
还有一个实现了IStaticDataSource接口的内部类
class StringDataSource : IStaticDataSource { public byte[] bytes { get; set; } public StringDataSource(MemoryStream ms) { bytes = ms.GetBuffer(); } public Stream GetSource() { Stream s = new MemoryStream(bytes); return s; } }
为了防止生成的压缩文件乱码,可以在using之上加上这两句代码
Encoding gbk = Encoding.GetEncoding("gbk"); ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage;
相关文章推荐
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint
- pd,使用powerdesigner的反向工程将数据库字典导出为rtf和Word文档,powerdesigner反向工程
- 使用itext导出word文档
- 开源Word读写组件DocX,通过word模板,导出用户简历使用示例
- [原创]开源Word读写组件DocX,通过word模板,导出用户简历使用示例
- 使用DTS导出到Access,FoxPro,Word ,Excel 不需要验证的大批量数据。
- iText的使用例子说明(利用Itext画模板导出word、pdf,纯java实现)
- 使用DBExportDoc导出mysql库为word表格
- 使用freemarker 导出word
- word组件 导出使用
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint
- 使用js将页面导出为word的简洁方法
- 使用COM来进行WORD或EXECEL导出操作,出现错误: 80070005 解决方法
- C# asp.net Word中使用书签插入图片导出
- 使用 OfficeIntegration.Word 实现lightswitch导出WORD的功能
- 使用GridView控件导出数据源的数据为Excel、Word或Text
- 使用PowerDesigner导出Word文档
- 使用freemarker 导出word供别人下载
- 在SSH2项目中实现使用Freemark导出Word文档
- jacob 使用模板导出word