如何使用EPPlus和MVC从服务器下载.xslx文件

我有一个Excel文件通过使用EPPlus在服务器上生成的文件是正确的,并使用window.location在本地机器上正常工作,但在部署到服务器时什么也不做。 我想通过MVC控制器返回一个FileStreamResult,但我不认为它的工作。 我正在使用ajax调用来访问控制器方法,但是在方法运行时无法input.done。

我一直在看ASP.NET MVC EPPlus下载Excel文件为我的C#参考。

脚本

function exportToExcel() { var batchName = $("#batchDateSelect option:selected").text(); var bID = $("#batchDateSelect").val(); var params = { BatchID: bID, BatchName: batchName }; $.post(path + "Export/ExportToExcel", params) .done(function (Data, textStatus, jqXHR) { var fileName = ""; ////window.location = path + "ExportFiles/"+fileName; }); } 

调节器

 public ActionResult ExportToExcel(int BatchID,string BatchName) { FileStreamResult FSR = DataAccess.ExportUtility.CreateExcelFile(BatchID, BatchName); return FSR; } 

EPPlus方法

 public static FileStreamResult CreateExcelFile(int batchid,string batchName) { string fileName = batchName + " Reason_Code_Export.xlsx"; var serverPath = HttpContext.Current.Server.MapPath("~/ExportFiles/"); DirectoryInfo outputDir = new DirectoryInfo(serverPath); FileInfo newfile = new FileInfo(outputDir.FullName + fileName); if (newfile.Exists) { newfile.Delete(); newfile = new FileInfo(outputDir.FullName + fileName); } Dictionary<string,int> MAData = PolicyDataAccess.GetMatchActionData(batchid); MemoryStream MS = new MemoryStream(); using (ExcelPackage package = new ExcelPackage(newfile)) { .......... ........ package.SaveAs(MS); } MS.Position = 0; var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; FileStreamResult FSR = new FileStreamResult(MS, contentType); FSR.FileDownloadName = fileName; return FSR; } 

什么是获取该文件最简单的方法?

我迟到了这个问题,但可能会对别人有帮助。

设置完Excel表格后, 如果不保存或者将其添加到MemoryStream中 ,只需将字节数组设置为packge.GetAsByteArray(),然后将其作为File而不是FileStreamResult返回

 var FileBytesArray = packge.GetAsByteArray(); return File(FileBytesArray, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename + ".xlsx"); 

EPPLUS给了我一些真正的麻烦。
而且我对MVC并不是很熟悉,但是我认为你要做的事情是直接写输出响应。 在这种情况下,我使用类似下面的内容。
而且我看到我留下了内存stream写入的注释。 这更接近你要求做的,但我目前没有在我的代码中使用它。 所以买家要小心。

。干杯。

  Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader( "Content-Disposition", "attachment; filename=ProposalRequest-" + fileName + ".xslx" ); Response.BinaryWrite( pck.GetAsByteArray() ); // myMemoryStream.WriteTo(Response.OutputStream); //works too Response.Flush(); Response.Close(); 

DougY发布的答案可能工作正常,但是我在发布之前find了解决scheme。

我不会把这个标记为答案,因为我确信有更好的办法,如果有人想发布或评论什么是最好的办法,那么不好的答案。

感谢DougY的回应

控制器的两种方法可能可以合并,但这只是它的结果。

调节器

 public static string ContentType { get; set; } public static string FilePath { get; set; } public static string FileName { get; set; } public static byte[] Bytes { get; set; } public void ExportToExcel(int BatchID,string BatchName)//is called first to set the variables { string contentType; byte[] bytes; string ret = DataAccess.ExportUtility.CreateExcelFile(BatchID, BatchName,out contentType, out bytes); ContentType = contentType; Bytes = bytes; FileName = ret[1]; } public ActionResult DownloadExcelFile()//is then called to download the file { return File(Bytes, ContentType, FileName); } 

ExportUtility类

 public static string[] CreateExcelFile(int batchid,string batchName,out string ContentType, out byte[] Bytes) { string fileName = batchName + " Reason_Code_Export.xlsx"; var serverPath = HttpContext.Current.Server.MapPath("~/ExportFiles/"); DirectoryInfo outputDir = new DirectoryInfo(serverPath); byte[] bytes; FileInfo newfile = new FileInfo(outputDir.FullName + fileName); if (newfile.Exists) { newfile.Delete(); newfile = new FileInfo(outputDir.FullName + fileName); } Dictionary<string,int> MAData = PolicyDataAccess.GetMatchActionData(batchid); MemoryStream MS = new MemoryStream(); ExcelPackage package; using (package = new ExcelPackage(newfile)) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(batchName); worksheet.Cells["A1"].Value = batchName + " Reason_Code_Export"; worksheet.Cells["A1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells["A1:B1"].Merge = true; worksheet.Cells["A1:B1"].Style.Font.Bold = true; worksheet.Cells["A2"].Value = "Reason Code"; worksheet.Cells["B2"].Value = "Number of Reason Codes Selected"; worksheet.Cells["A2:B2"].Style.Font.Bold = true; int row = 3; int col = 1; foreach (KeyValuePair<string,int> MA in MAData) { worksheet.Cells[row, col].Value = MA.Key; worksheet.Cells[row, col + 1].Value = MA.Value; row++; } worksheet.Column(1).Width = 34.29; worksheet.Column(2).Width = 34.29; package.Workbook.Properties.Title = batchName + " Reason_Code_Export"; package.Workbook.Properties.Author = "Intranet Application: Unclaimed Properties"; package.Workbook.Properties.Company = "Assurity Life 2013"; Bytes = package.GetAsByteArray(); //package.SaveAs(newfile);//MS); } MS.Position = 0; var rl = serverPath + fileName; var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; ContentType = contentType; FileStreamResult FSR = new FileStreamResult(MS, contentType); FSR.FileDownloadName = fileName; string[] ret = new string[2]; ret[0] = serverPath; ret[1] = fileName; return ret; }