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



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

public void Excel(字符串文件名){DAOSolitacao dao=新的DAOSolicatao();System.Threading.Thread.Sleep(5000);var乘积=dao。GetSolicitacoes();var grid=new GridView();网格DataSource=产品;网格DataBind();Response.ClearContent();Response.Buffer=true;Response.AddHeader("内容处置",string.Format("附件;文件名={0}",文件名));Response.ContentType="application/ms-excel";响应。字符集=";StringWriter sw=新StringWriter();HtmlTextWriter htw=新的HtmlTextWriter(sw);网格RenderControl(htw);Response.Output.Write(sw.ToString());Response.Flush();Response.End();}

如何将此xls文件返回到zip文件中?

谷歌!是你的朋友!

DotNetZip库

已经有一个内置类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);
}

最新更新