Exporting HTML Table to Excel via Page Method

I have the following code on the client side for retrieving data from the HTML Table and then send it to the server through the page method:function dtExportToCSV(dataTable) var elements = dtDataToJSON(dataTable); var headers = dtHeadersToJSON(tableSelector); jQuery.ajax( type: “POST”, url: “Report.aspx/exportToCSV”, data: “‘elements’: ” + elements + “, ‘headers’: ” …

via Asp.Net Developed Tutorials » Search Results » ajax:

Exporting HTML Table to Excel via Page Method

I have the following code on the client side for retrieving data from the HTML Table and then send it to the server through the page method:
function dtExportToCSV(dataTable)
var elements = dtDataToJSON(dataTable);
var headers = dtHeadersToJSON(tableSelector);

jQuery.ajax(
type: “POST”,
url: “Report.aspx/exportToCSV”,
data: “‘elements’: ” + elements + “, ‘headers’: ” + JSON.stringify(headers) + “”,
contentType: “application/json; charset=utf-8″,
dataType: “json”,

success: function(msg)
if (msg.d)
else

},

error: function(xhr, ajaxOptions, thrownError)
alert(xhr.statusText);

});
}
Then I use the following code on the server side, to export the retrieved data to CSV file.
///

///
///

//////[WebMethod(EnableSession=true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static bool exportToCSV(List elements, List headers)

try

string attachmentType = “attachment; filename=ShortageReport.csv”;

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader(“content-disposition”, attachmentType);
HttpContext.Current.Response.ContentType = “text/csv”;
HttpContext.Current.Response.AddHeader(“Pragma”, “public”);

writeHeadersInfo(headers);

HttpContext.Current.Response.End();

catch (Exception ex)

Console.WriteLine(ex.Message);

return false;
}
But I get this expcetion: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
Does anyone knows how to handle this issue?
Any help would be highly appreciated! Thanks in Advance!
~ Eder Qui?ones
……………………………………….

///

///
///

/// /// [WebMethod(EnableSession=true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static bool exportToCSV(List elements, List headers)

try

string attachmentType = “attachment; filename=Shortage Report.csv”;

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();

HttpContext.Current.Response.AddHeader(“Content-Disposition”, attachmentType);
HttpContext.Current.Response.ContentType = “text/csv”;

writeHeadersInfo(headers);
writeBodyInfo(headers, elements);

HttpContext.Current.ApplicationInstance.CompleteRequest();

catch (Exception ex)

Console.WriteLine(ex.Message);

return true;
}
But it doesn’t show up the save file dialog…

For more info: Exporting HTML Table to Excel via Page Method

Asp.Net Developed Tutorials » Search Results » ajax

Exporting HTML Table to Excel via Page Method

Share this post:

Related Posts

Leave a Comment