文件下载 - 从mvc控制器下载excel文件时返回空白文件

时间:2015-05-18 06:38:51

标签: asp.net-mvc

 public ActionResult ExportLeadTimingReport(JLRReportSearchCriteriaViewModel searchCriteria)
    {
        OperationResponse<DataTable> LeadTimingReport = new OperationResponse<DataTable>();
        LeadTimingReport.Value = new DataTable();
          byte[] file = LeadManager.GetJLRLeadTimingReportForExport(searchCriteria);

                HttpContext.Response.ClearContent();
                HttpContext.Response.Clear();
                HttpContext.Response.Buffer = true;
                HttpContext.Response.AddHeader("content-disposition", "attachment; filename=Export.xlsx");
                HttpContext.Response.ContentType = "application/force-download";
                HttpContext.Response.Charset = "";

                HttpContext.Response.BinaryWrite(file);
                HttpContext.Response.End();

                return null;
    }

返回空的excel文件。但是二进制数组包含数据,但是当它被调用时它会给出空白的excel文件。

1 个答案:

答案 0 :(得分:1)

请修改下面给出的代码

public ActionResult ExportLeadTimingReport(JLRReportSearchCriteriaViewModel searchCriteria)
    {
       OperationResponse<DataTable> LeadTimingReport = new OperationResponse<DataTable>();
       LeadTimingReport.Value = new DataTable();
       byte[] file = LeadManager.GetJLRLeadTimingReportForExport(searchCriteria);

       this.Response.ContentType = "application/vnd.ms-excel";
       return File(file , "application/vnd.ms-excel");
}