How do I convert an html table to an Excel File?

I'm trying to get my page Data.aspx to return an html table, but I want that table to be in an excel format. This is my code so far.

In the Data.aspx Load:

Response.Clear()

    Select Case lstrCmd
        Case "summary"
            Response.Write(Summary())
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Case "export"
            Response.ContentType = "application/ms-excel"
            Response.AddHeader("Content-Disposition", "attachment;filename=" & _
                    mstrProjectName & ".xls")
            Response.Write(SummaryExport())
    End Select

    Response.End()

So the add header is where I'm puzzled. This is something another peice of my software uses, but I'm not sure if it will work..

the jquery that calls this is using ajax:

     function btnSubmitExport_Click() {
        var compID = $("#ddlCompanies").val();
        var projectID = $("#ddlProjects").val();
        var startDate = $("#txtStartDate").val();
        var endDate = $("#txtEndDate").val();
        var compName = $("#ddlCompanies :selected").text();
        var projectName = $("#ddlProjects :selected").text();
        $("#content").html("<div class='loading'>loading...</div>");

        $.ajax({
            url:        "Data.aspx",
            data:       {
                            CompanyID:  compID,
                            ProjectID:  projectID,
                            CompanyName: compName,
                            ProjectName: projectName,
                            StartDate:  startDate,
                            EndDate:    endDate,
                            Cmd:        "export"
                        },                
            success:    function(html) {
                             $("#content").html(html);       
                        },
            error:      function(response) {
                    alert(response);
            }
        });
     }

So, typically, when I request this data, I just throw the resulting html into a "content" div on my default.aspx page.. but I want that html to be an excel file!

Any hints or thoughts on this would be helpful.

I see the same question in other places, but not any answers that help me with my existing code..

Answers


If you have an html text contains Table tag in it, simple save it with extension of XLS and Microsoft Excel will open it like a charm! And also as I've seen, you want user to download the file, not to show its content in the page. You should redirect page on your ASPX page directly (without using JQuery). I'd suggest you to use network monitor in the developer tools to see what's happening.

Cheers


Need Your Help

Why constant data member of a class need to be initialized at the constructor?

c++ constructor class-constants

I want to know why constant data member of a class need to be initialized at the constructor and why not somewhere else? What is the impact of doing so and not doing so?

HttpContext.Current represents the current context of a user… what is meant by current context?

c# asp.net security

HttpContext.Current returns an IPrincipal object that represents security context of the current user. I understand principal object combines the identity of a user with other information, such as ...