Downloading Excel file after creating using EPPlus

后端 未结 4 1883
再見小時候
再見小時候 2021-01-02 01:20

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?

<
相关标签:
4条回答
  • 2021-01-02 01:37

    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());
          }
    }         
    
    0 讨论(0)
  • 2021-01-02 01:42

    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);
    }  
    
    0 讨论(0)
  • 2021-01-02 01:43

    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/

    0 讨论(0)
  • 2021-01-02 01:45

    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.

    0 讨论(0)
提交回复
热议问题