【问题标题】:ASP.NET MVC returning an xls file inside an zip fileASP.NET MVC 在 zip 文件中返回 xls 文件
【发布时间】:2013-06-21 07:00:43
【问题描述】:

我有一个返回 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 文件中?

【问题讨论】:

    标签: asp.net-mvc xls zipfile


    【解决方案1】:

    谷歌!是你的朋友!

    DotNetZip Library

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢!第二个选择更适合我!
      猜你喜欢
      • 2014-11-15
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      相关资源
      最近更新 更多