我已經從HTML表中檢索數據,然後通過網頁方式發送到服務器上的客戶端下面的代碼:導出HTML表到Excel
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);
}
});
}
然後我用的是以下代碼在服務器端,將檢索到的數據導出到CSV文件。
/// <summary>
///
/// </summary>
/// <param name="elements"></param>
/// <param name="headers"></param>
[WebMethod(EnableSession=true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static bool exportToCSV(List<object> elements, List<string> 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;
}
但我得到這個expcetion:無法因爲代碼優化或本機框上調用堆棧的頂部,以評估表達。
有沒有人知道如何處理這個問題?
任何幫助將不勝感激! 在此先感謝!
〜埃德爾醌
異常來自哪條線? – Ender 2010-12-03 22:03:36