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..
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