Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading Excel file after creating using EPPlus

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();
  }
}
like image 459
Arianule Avatar asked Jan 20 '15 15:01

Arianule


People also ask

How do you make an Excel file downloadable?

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.

How can create and download Excel file in ASP NET MVC?

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.

Does EPPlus support XLS?

EPPlus does not work with the XLS format.


3 Answers

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());
      }
}         
like image 185
Alex Art. Avatar answered Oct 16 '22 14:10

Alex Art.


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.

like image 44
Steven V Avatar answered Oct 16 '22 16:10

Steven V


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);
}  
like image 26
Pankaj Kapare Avatar answered Oct 16 '22 15:10

Pankaj Kapare