0
votes

I have an asp.net mvc action that returns an xls file:

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

How can I make this xls file return into an zip file ?

2

2 Answers

0
votes
0
votes

There is already a build in class ZipFile

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

that you can try

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

}