Introduction
We can use multiple approaches to export multiple Excel files. Here's how you can achieve that.
- Zip file download.
- multiple worksheets in a single Excel file.
- Using base64string, download multiple files next article.
- API calls were made multiple times to get multiple files next article.
Download multiple files using Approach Zip file download.
In this example, we demonstrate how to generate multiple Excel files dynamically based on data, compress them into a ZIP archive, and return the ZIP file as a byte array. This is a common use case in applications where we need to export data in bulk, compress it, and make it downloadable via a web API or service. We'll use the EPPlus library to generate Excel files and System.IO.Compression to handle the ZIP compression.
Web API .Net
Step 1. Install nuget package.
- EPPlus: For Excel file creation.
- System.IO.Compression: For zipping files.
Step 2. Code Breakdown.
1. Method Overview: ExportZipFile().
public byte[] ExportZipFile()
{
// Data for the first Excel file
List<Info> infos = new List<Info>()
{
new Info { Name = "sfas", Description = "Dev", Address = "LA" },
new Info { Name = "VK", Description = "Man", Address = "IND" },
new Info { Name = "LAL", Description = "Dev", Address = "NW" }
};
// Data for the second Excel file
List<Info> files = new List<Info>()
{
new Info { Name = "sfas1", Description = "Dev", Address = "LA" },
new Info { Name = "VK1", Description = "Man", Address = "IND" },
new Info { Name = "LAL1", Description = "Dev", Address = "NW" },
new Info { Name = "LAL2", Description = "Dev", Address = "NW" }
};
// Generate the Excel files
var excelFiles = new List<(string fileName, byte[] content)>
{
("File1.xlsx", GenerateExcelFile(infos)),
("File2.xlsx", GenerateExcelFile(files))
};
// Create a ZIP archive in memory and add the Excel files to it
using (var zipStream = new MemoryStream())
{
using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
foreach (var excelFile in excelFiles)
{
// Create a ZIP entry for each Excel file
var zipEntry = archive.CreateEntry(excelFile.fileName, CompressionLevel.Fastest);
using (var entryStream = zipEntry.Open())
using (var fileStream = new MemoryStream(excelFile.content))
{
// Write the Excel file content to the ZIP entry
fileStream.CopyTo(entryStream);
}
}
}
// Return the ZIP file as a byte array
return zipStream.ToArray();
}
}
2. Generating Excel Files: GenerateExcelFile(List<Info> infos).
This method uses the EPPlus library to generate an Excel file from a list of Info objects. It adds the data into an Excel worksheet and returns the generated Excel file as a byte array.
private byte[] GenerateExcelFile(List<Info> infos)
{
var currentRowIndex = 2; // Start at row 2 to leave row 1 for headers
// Set the license context for EPPlus (required since version 5)
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
// Create a new Excel package
using var package = new OfficeOpenXml.ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Info");
// Create headers in the first row
worksheet.Cells["A1"].Value = "Name";
worksheet.Cells["B1"].Value = "Description";
worksheet.Cells["C1"].Value = "Address";
// Fill the worksheet with data from the 'infos' list
infos.ForEach(info =>
{
worksheet.Cells[currentRowIndex, 1].Value = info.Name;
worksheet.Cells[currentRowIndex, 2].Value = info.Description;
worksheet.Cells[currentRowIndex, 3].Value = info.Address;
currentRowIndex++;
});
// Convert the Excel package to a byte array
return package.GetAsByteArray();
}
Controller
- Service Call: _fileExportService.ExportZipFile() generates a ZIP file in memory (likely returning a byte[] array as you implemented earlier).
- File Return: The File() method is used to send the generated file as a response to the client. It accepts three parameters:
- zipFile: The byte array representing the ZIP file.
- "application/zip": The content type of the response (MIME type).
- Filename: The name of the file as it will appear when the user downloads it.
[HttpGet("export-zip")]
public IActionResult ExportZipFile()
{
var zipFile = _fileExportService.ExportZipFile();
var fileName = "ExportFiles.zip";
return File(zipFile, "application/zip", fileName);
}
Explanation of the Process
- Data Preparation: Two lists of Info objects are created, representing data for two different Excel files. Each Info object contains a Name, Description, and Address.
- Excel File Generation: The GenerateExcelFile() method takes a list of Info objects and generates an Excel file with that data. This method creates an Excel worksheet, adds column headers (Name, Description, Address), and populates each row with the data from the list.
- ZIP File Creation: The method ExportZipFile() then uses System.IO.Compression.ZipArchive to create an in-memory ZIP archive. It iterates over the generated Excel files and adds each one to the ZIP file using the CreateEntry() method.
- Returning the ZIP File: Finally, the method converts the entire ZIP archive into a byte array and returns it. This byte array can be returned as a file download in a web API or saved to disk.





Join the conversation! Your thoughts help the community grow.