c# - 通过页面方法将 HTML 表格导出到 Excel

标签 c# asp.net jquery ajax pagemethods

我在客户端有以下代码,用于从 HTML 表中检索数据,然后通过页面方法将其发送到服务器:

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;
}

但我得到了这个异常:无法评估表达式,因为代码已优化或 native 框架位于调用堆栈的顶部。

有谁知道如何处理这个问题?

任何帮助将不胜感激! 提前致谢!

~ Eder Quiñones

最佳答案

    /// <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=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;
    }

但它没有显示保存文件对话框...

关于c# - 通过页面方法将 HTML 表格导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4217476/

相关文章:

javascript - 以编程方式选择 jqGrid 中的所有行?

javascript - 使用嵌入在自己网站中的小部件从 Soundcloud 获取歌曲

c# - HttpWebRequest 对象的诊断转储

C# MVC 在默认路由上使用参数

c# - WPF Treeview HierarchicalDataTemplate ItemTemplateSelector

asp.net - 菜单控件在环境之间构建和呈现不同

c# - List<string> 复杂排序

c# - ASP.net MVC 4 - CaSTLe Windsor - 找不到方法 : 'System. Web.Http.Services.DependencyResolver

c# - 在我的事件处理程序范围内遇到问题(ASPX .NET 3.5)

jquery - 获取jquery元素数组的坐标