I am using the EPPlus library to generate an excel file which I successfully save in a folder on the server.
How can download this file to my local machine?
This is my code
public void CreateExcelFirstTemplate()
{
var fileName = "C:\ExcelDataTest\ExcellData.xlsx";
var file = new FileInfo(fileName);
using (var package = new OfficeOpenXml.ExcelPackage(file))
{
var worksheet = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == "Attempts");
worksheet = package.Workbook.Worksheets.Add("Assessment Attempts");
worksheet.Row(1).Height = 20;
worksheet.TabColor = Color.Gold;
worksheet.DefaultRowHeight = 12;
worksheet.Row(1).Height = 20;
worksheet.Cells[1, 1].Value = "Employee Number";
worksheet.Cells[1, 2].Value = "Course Code";
var cells = worksheet.Cells["A1:J1"];
var rowCounter = 2;
foreach (var v in userAssessmentsData)
{
worksheet.Cells[rowCounter, 1].Value = v.CompanyNumber;
worksheet.Cells[rowCounter, 2].Value = v.CourseCode;
rowCounter++;
}
worksheet.Column(1).AutoFit();
worksheet.Column(2).AutoFit();
package.Workbook.Properties.Title = "Attempts";
package.Save();
}
}
Select File > Save As > Download a Copy. If Excel asks whether to open or save the workbook, select Save. Note: If you select Open instead of Save, the workbook will open in Protected View. Depending on your browser, you may not be asked this.
Step 1: Create a new ASP.NET web application project. Step 2: Complete the New ASP.NET Web Application – CreateXlsIOSample dialog: Select MVC. Click OK.
EPPlus does not work with the XLS format.
If you are generating this file on each request you don't need to save it on the server:
public void CreateExcelFirstTemplate()
{
var fileName = "ExcellData.xlsx";
using (var package = new OfficeOpenXml.ExcelPackage(fileName))
{
var worksheet = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == "Attempts");
worksheet = package.Workbook.Worksheets.Add("Assessment Attempts");
worksheet.Row(1).Height = 20;
worksheet.TabColor = Color.Gold;
worksheet.DefaultRowHeight = 12;
worksheet.Row(1).Height = 20;
worksheet.Cells[1, 1].Value = "Employee Number";
worksheet.Cells[1, 2].Value = "Course Code";
var cells = worksheet.Cells["A1:J1"];
var rowCounter = 2;
foreach (var v in userAssessmentsData)
{
worksheet.Cells[rowCounter, 1].Value = v.CompanyNumber;
worksheet.Cells[rowCounter, 2].Value = v.CourseCode;
rowCounter++;
}
worksheet.Column(1).AutoFit();
worksheet.Column(2).AutoFit();
package.Workbook.Properties.Title = "Attempts";
this.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
this.Response.AddHeader(
"content-disposition",
string.Format("attachment; filename={0}", "ExcellData.xlsx"));
this.Response.BinaryWrite(package.GetAsByteArray());
}
}
Instead of using package.Save()
you can use package.GetAsByteArray()
which will return a byte array which you can then stream using FileResult
or FileContentResult
from the MVC action to trigger a file download. This method will allow you to download the file without saving it to the server first.
Here is sample action to download file. Feel free to modify it as per your requirement.
public FileActionResult DownloadMyFile()
{
var filePath = "C:\ExcelDataTest\ExcellData.xlsx";
var fileName = "ExcellData.xlsx";
var mimeType = "application/vnd.ms-excel";
return File(new FileStream(filePath, FileMode.Open),mimeType, fileName);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With