24672

ASP.NET MVC returning an xls file inside an zip file

Question:

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 ?

Answer1:

Google! Is your friend!

<a href="http://dotnetzip.codeplex.com/" rel="nofollow">DotNetZip Library</a>

Answer2:

There is already a build in class ZipFile

<a href="http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx</a>

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

Recommend

  • Exporting datatable to excel file with save dialog
  • Response.Clear and Response.ClearContent in Spring MVC
  • Filename and mime problems - ASP.NET Download file (C#)
  • How to prevent css missing when exporting some html to excel file?
  • add string item to datalist asp.net
  • How can I hide empty columns in a gridview without knowing which will be empty?
  • How to stream byte array image?
  • In C#, how can I reference a specific product record based on a button that's clicked in a grid
  • How to implement cascading DropDownList ASP.Net control? [closed]
  • EntityDataSource query inner join
  • gridview rendered in div
  • Binding a list with another list with WPF
  • yii2 sorting in related model
  • SCons libraries and sub-libraries
  • Bind drop down list with Dictionary
  • How to delete a newline using \\b
  • How to start server for Selenium grid Java Maven setup
  • Shopify API CARTS - Changing line_item line_price for price Override
  • How can I make a 100% stacked chart with nvd3?
  • Why does Apple use assign rather than weak to store a delegate?
  • Allocating a 2D contiguous array within a function
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Returning the auto incrementing value after an insert using slick
  • Dynamically load css stylesheet and wait for it to load
  • Sending HTML Form Data to Spring REST Web Service
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • Control modification in presentation layer
  • How to add date and time under each post in guestbook in google app engine
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • How to delete a row from a dynamic generate table using jquery?
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize