Download Excel file via AJAX MVC

后端 未结 14 1799
说谎
说谎 2020-11-22 02:30

I have a large(ish) form in MVC.

I need to be able to generate an excel file containing data from a subset of that form.

The tricky bit is that this shouldn

14条回答
  •  再見小時候
    2020-11-22 03:24

    On Submit form

    public ActionResult ExportXls()
    {   
     var filePath="";
      CommonHelper.WriteXls(filePath, "Text.xls");
    }
    
     public static void WriteXls(string filePath, string targetFileName)
        {
            if (!String.IsNullOrEmpty(filePath))
            {
                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.Charset = "utf-8";
                response.ContentType = "text/xls";
                response.AddHeader("content-disposition", string.Format("attachment; filename={0}", targetFileName));
                response.BinaryWrite(File.ReadAllBytes(filePath));
                response.End();
            }
        }
    

提交回复
热议问题