Articles in this section
Category / Section

Download multiple Excel documents in a single compressed file in ASP.NET MVC

3 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, convert Excel document to PDF files. In this article, we are going to see how to create more Excel files in an application and download them as a single compressed file in ASP.NET MVC platform.

For security reasons, MVC platform doesn’t allow multiples file to download in a single response. However, with XlsIO you can generate more files and save them into a temporary location, then compress those files to save all at once in a single download. Using the Compression library you download the final compressed file in a browser. Let see how to achieve this in the article.

Steps to save all the Excel files to 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

Create a new C# ASP.NET Web Application project

Step 2: Name the project.

Name the project

Name the project

Step 3: Select Web Application pattern as Model-View-Controller for the project.

Select the web application pattern as MVC

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

Install NuGet package as reference to the project

Step 5: A default controller with named HomeController.cs gets added on creation of 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 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 new action method CreateDocument in HomeController.cs and include the below code snippet to save all the Excel files to Zip Archive and download the compressed file in 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 worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions etc. with code examples.

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied