[WebMethod] public String ExportToExcel() { try { // DataSet ds; HttpResponse response = HttpContext.Current.Response; // first let's clean up the response.object response.Clear(); response.Charset = ""; response.ContentEncoding = System.Text.Encoding.Default; // set the response mime type for excel response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"ExportTest.xls\""); // create a string writer using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { response.Write(sw.ToString()); response.End(); } } return "Exported"; } catch (Exception ex) { return ex.Message; } }
$.ajax({ url:"WebService.asmx?op=ExportToExcel" , type: "GET" ,contentType: "application/vnd.ms-excel" , data: {} , success: function (res) { alert("Success"); } , error: function (res) { alert("Error"); } });
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)