2013-10-04 89 views
2

我有一個使用EPPlus在服務器上生成的Excel文件,該文件是正確創建的,並且使用window.location在本地計算機上工作正常,但在部署時不執行任何操作到服務器。我試圖通過MVC控制器返回一個FileStreamResult,但我認爲它不工作。我使用ajax調用來訪問控制器方法,但在方法運行時無法輸入.done。如何使用EPPlus和MVC從服務器下載.xslx文件

我一直在尋找ASP.NET MVC EPPlus Download Excel File爲我的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; 
    } 

什麼是獲取該文件的最簡單的方法?

回答

1

    EPPLUS給了我一些真正的麻煩。
而且我對MVC並不是很熟悉,但我認爲你希望按照直接寫入輸出響應的方式做一些事情。在這種情況下,我使用類似下面的內容。
我看到我留下了內存流寫入的註釋。這更接近你要求做的,但我目前不在我的代碼中使用它。所以買家要小心。

.cheers。

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

謝謝,這有助於解決與「eprlus」編輯模板時「損壞」文件相關的問題 – blfuentes

-1

DougY發帖的答案可能工作正常,但是我在發佈之前找到了解決方案。

我不會將此標記爲已回答,因爲我確定有更好的方法,如果有人想發佈或評論什麼是最好的方法,那麼不好標記答案。

感謝您的答覆DougY

控制器的2種方法也許可以合併,但是這只是它是如何結束了。

控制器

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 class

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

,我遲到了這個問題,但可能會成爲別人的幫助。

你安裝後的Excel工作表,然後不保存或將其添加到MemoryStream的,才使字節packge.GetAsByteArray(),並從你的行動換來的卻的文件,而不是FileStreamResult的陣列。

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