Downloading Excel file after creating using EPPlus

亡梦爱人 提交于 2019-11-30 02:50:00

问题


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();
  }
}

回答1:


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());
      }
}         



回答2:


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.




回答3:


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);
}  



回答4:


You can do like this:

excel.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(excel.GetAsByteArray());

For detailed blog: http://sforsuresh.in/generating-and-formatting-excelsheet-in-c-using-epplus/



来源:https://stackoverflow.com/questions/28048835/downloading-excel-file-after-creating-using-epplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!