0

我有一个返回 xls 文件的 asp.net mvc 操作:

公共无效Excel(字符串文件名)
{
    DAOSolicitacao dao = new DAOSolicitacao();

    System.Threading.Thread.Sleep(5000);
    var products = dao.GetSolicitacoes();

    var grid = new GridView();
    grid.DataSource = 产品;
    grid.DataBind();

    Response.ClearContent();
    响应缓冲区=真;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename));
    Response.ContentType = "应用程序/ms-excel";
    Response.Charset = "";

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    响应。结束();
}

我怎样才能使这个 xls 文件返回到一个 zip 文件中?

4

2 回答 2

0

ZipFile 类中已经有一个构建

http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx

你可以试试

using (ZipFile zipFile = new ZipFile())
{
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}",   filename));
    Response.ContentType = "application/ms-excel";
    Response.Charset = "";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid.RenderControl(htw);
    zipFile.Save(Response.Output.Write(sw.ToString())); 
    return File(zipFile.GetBytes(), "application/zip", downloadFileName);

}
于 2013-06-24T17:35:49.323 回答
0

谷歌!是你的朋友!

DotNetZip 库

于 2013-06-24T17:24:28.910 回答