Download multiple Excel documents in a single compressed file in ASP.NET MVC
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It can also convert Excel documents to PDF files. In this article, we are going to see how to create multiple Excel files in an application and download them as a single compressed file in the ASP.NET MVC platform.
For security reasons, the MVC platform doesn’t allow multiple files to be downloaded in a single response. However, with XlsIO, you can generate multiple files, save them to a temporary location, then compress those files to save all at once in a single download. Using the Compression library, you can download the final compressed file in a browser. Let us see how to achieve this in the article.
Steps to save all the Excel files to a Zip Archive and download the compressed file in a browser:
Step 1: Create a new C# ASP.NET Web application.
Create a new C# ASP.NET Web Application project.
Step 2: Name the project.
Name the project
Step 3: the Web Application pattern as Model-View-Controller for the project.
Select the web application pattern as MVC
Step 4: Install the Syncfusion.XlsIO.AspNet.MVC5 NuGet package as reference to your application from NuGet.org.
Install NuGet package as reference to the project
Step 5: A default controller named HomeController.cs gets added upon the creation of the project. Include the following namespaces in this HomeController.cs file.
C#
using Syncfusion.XlsIO; using System.IO.Compression; using System.Web.Mvc;
Step 6: A default action method named Index will be present in HomeController.cs. Right-click on the Index method and select "Go To View" where you will be directed to its associated view page Index.cshtml.
Step 7: Add a new button in the Index.cshtml as shown below.
CSHTML
@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get); { <div> <input type="submit" value="Create Document" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
Step 8: Add a a new action method CreateDocument in HomeController.cs and include the below code snippet to save all the Excel files to a Zip Archive and download the compressed file in a browser.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; IWorkbook workbook; int[] products = new int[] { 1, 2, 3, 4, 5 }; foreach (int selectedProduct in products) { workbook = application.Workbooks.Create(1); workbook.Worksheets[0].Range["A1"].Text = "Book" + selectedProduct.ToString(); workbook.SaveAs(Server.MapPath("~/App_Data/Book" + selectedProduct.ToString() + ".xlsx")); } var archive = Server.MapPath("~/archive.zip"); var temp = Server.MapPath("~/App_Data"); if (System.IO.File.Exists(archive)) { System.IO.File.Delete(archive); } ZipFile.CreateFromDirectory(temp, archive); Response.ContentType = "application/zip"; Response.AddHeader("Content-Disposition", "attachment; filename=archive.zip"); Response.TransmitFile(archive); return View("Index"); }
A complete working example to save multiple Excel documents in a single compressed file can be downloaded from SaveExcelFiles.zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to set row height when using WrapText in XlsIO.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!