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

TCL 8.3: syntax error in expression “a eq b”

tcl alias equals syntax-error

I have to run TCL code written for version 8.5 with the interpreter of version 8.3.

Error C2143: syntax error : missing ';' before 'namespace'

c++ syntax sstream

I am VERY new to C++ and Open GL and I have been trying to display 3D objects in a scene. it worked fine with one but when I tried to alter my code to add a second, my code regarding the HUD text s...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.