2014-11-05 7 views
0

Excel 파일을 생성하는 데 EPPLUS를 사용하고 있습니다. 서버에 저장하는 코드가 있지만 클라이언트가 Excel 시트를 다운로드하는 방법을 알 수 없습니다. 사용자가 단추를 클릭하면 브라우저에서 설정 한 곳으로 다운로드됩니다. 나는 그것은클라이언트가 Excel 파일을 다운로드하도록 허용

$('#ExcelExport').on('click', function() { 
    $.post("/Reports/ExportToExcel", function (data) { 
     alert("The Export was Succussful"); 
    }) 
    .fail(function() { 
     alert("The Export has Failed"); 
}); 

JQuery와 방화범이 끌려 또는 피들러

에서 오류를 던지고 아니에요 ... 내가 일을 얻을 수없는 클라이언트 다운로드 코드 here을 발견 C#

public ActionResult ExportToExcel() 
{ 

    using (ExcelPackage package = new ExcelPackage()) 
    { 
     // Get Data 
     var excelData = _repository.ExcelData(); 

     //Basic Info 
     package.Workbook.Properties.Author = "MEO System"; 
     package.Workbook.Properties.Title = "PendingMEOs"; 

     //Excel worksheet 
     package.Workbook.Worksheets.Add("Pending MEOs"); 
     ExcelWorksheet ws = package.Workbook.Worksheets[1]; // 1 is the position of the worksheet 
     ws.Name = "Pending MEOs"; 

     //adds the data to the excel sheet and formats the data 
     ws.Cells.LoadFromCollection(excelData, true, OfficeOpenXml.Table.TableStyles.Medium6); 

     //sets the cell width to fit the contents 
     ws.Cells["A1:L" + excelData.Count()].AutoFitColumns(); 

     // Save the Excel file 
     Response.Clear(); 
     Response.AddHeader("content-disposition", "attachment; filename=PendingMEOS.xlsx"); 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.BinaryWrite(package.GetAsByteArray()); 
     Response.End(); 
    } 

    return Json(new { success = true }); 
} 

코드를 서버에 저장하면 작동하지만 내가 원하는 것은 작동하지 않습니다.

string path = "C:\\PendingMEOS.xlsx"; 

Byte[] bin = package.GetAsByteArray(); 
System.IO.File.WriteAllBytes(path, bin); 

답변

1

문제는 응답 스트림에 직접 쓰고 있지만 Json 결과를 반환한다는 것입니다. 그것이 성공하지만 브라우저가 다운로드를 알려하지 않습니다 말한다 나는 내가하지를 찾을 수없는 나는`;

) 새로운 EmptyResult를 (반환`추가 한 후 MVC Controller Using Response Stream

+0

에서보세요 그것이 실제로 어디서나 다운로드되고 있다고 생각합니다. 다운로드 할 수있는 코드가 누락 되었습니까? – joetinger

+1

게시물 성공 처리기가 성공 메시지를 경고하고 데이터로 아무 것도하지 않았습니다. 파일을 다운로드하려는 경우/Reports/ExportToExcel에 대한 링크를 직접 시도하십시오. – Vince

+0

정확히 내가 찾던 것을 고맙습니다! – joetinger

관련 문제