ASP.NET MVC在zip文件中返回xls文件

时间:2013-06-24 17:09:20

标签: asp.net-mvc xls zipfile

我有一个asp.net mvc动作,它返回一个xls文件:

public void Excel(string filename)
{
    DAOSolicitacao dao = new DAOSolicitacao();

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

    var grid = new GridView();
    grid.DataSource = products;
    grid.DataBind();

    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);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}

如何将此xls文件恢复为zip文件?

2 个答案:

答案 0 :(得分:0)

谷歌!是你的朋友!

DotNetZip Library

答案 1 :(得分: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);

}