I'm following the Export to Excel demo and I'm having trouble adapting it to my ASP.NET Web Forms project. I'm using an .asmx Web Service for my Web Methods.
I'm getting 500 internal server errors related to the following.
One issue is using two Web Methods with the same name in Web Forms. The POST method will run correctly if the GET method is commented out. With both uncommented, I get the error "Item has already been added. Key in dictionary: 'Excel'; Key being added: 'Excel'."
The other issue is returning the correct file in the GET Method. FileContentResult and "return File(new UTF8Encoding().GetBytes(contents), "text/csv", filename);" doesn't compile in Web Forms because they are MVC.
Here is my Export To Excel code.
JavaScript
toolbar: {
items: [
//Export to Excel buttons
{ type: 'button', label: "Export to XML", icon: 'ui-icon-document', listeners:
[{ "click": function (evt) {
$grid.pqGrid("exportExcel", { url: "EditJobListWS.asmx/Excel", sheetName: "Gird_Export" });
}
}]
},
{ type: 'button', label: "Export to CSV", icon: 'ui-icon-document', listeners:
[{ "click": function (evt) {
$grid.pqGrid("exportCsv", { url: "EditJobListWS.asmx/Excel" });
}
}]
}
]
},
ASP.NET C# Web Service (.asmx)
//AJAX POST
[WebMethod(EnableSession = true)]
public String Excel(String extension, String excel)
{
if (extension != "csv" && extension != "xml") { throw new Exception("Unsupported extension"); }
String filename = "Grid." + extension;
Session["ExportExcel"] = excel;
return filename;
}
//AJAX GET
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public Object Excel(String filename)
{
String contents = Session["ExportExcel"].ToString();
if (filename.EndsWith(".csv"))
{
//return File(new UTF8Encoding().GetBytes(contents), "text/csv", filename);
}
else if (filename.EndsWith(".xml"))
{
//return File(new UTF8Encoding().GetBytes(contents), "text/xml", filename);
}
else { throw new Exception("Unknown extension"); }
return contents;
}