使用 EPPlus 创建后正在下载 Excel 文件

Downloading Excel file after creating using EPPlus

我正在使用 EPPlus 库生成一个 excel 文件,我成功地将其保存在服务器上的一个文件夹中。

如何将此文件下载到我的本地计算机?

这是我的代码

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

这是下载文件的示例操作。随意根据您的要求进行修改。

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

您可以使用 package.GetAsByteArray() 而不是 package.Save(),这将 return 一个字节数组,然后您可以使用 FileResultFileContentResult 从触发文件下载的 MVC 操作。此方法将允许您下载文件而无需先将其保存到服务器。

如果您在每次请求时生成此文件,则无需将其保存在服务器上:

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

你可以这样做:

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

详细博客:http://sforsuresh.in/generating-and-formatting-excelsheet-in-c-using-epplus/