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

      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 = ;
ms.Read(buffer, , 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;
05-11 20:07