Author Topic: Export To Excel (ASP.NET Web Forms)  (Read 3916 times)

SMV93

  • Newbie
  • *
  • Posts: 8
    • View Profile
Export To Excel (ASP.NET Web Forms)
« on: October 06, 2017, 12:06:30 am »
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
Code: [Select]
        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)
Code: [Select]
        //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;
        }

« Last Edit: October 06, 2017, 12:12:22 am by SMV93 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Export To Excel (ASP.NET Web Forms)
« Reply #1 on: October 06, 2017, 08:57:16 pm »
Local export via filesaver.js can also be done which is easier.

https://paramquery.com/pro/demos/export_local

SMV93

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Export To Excel (ASP.NET Web Forms)
« Reply #2 on: October 07, 2017, 03:10:39 am »
That looks much more straight forward. Is exportData only available on the Pro version?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Export To Excel (ASP.NET Web Forms)
« Reply #3 on: October 09, 2017, 09:28:19 pm »
Sorry it won't work with free version as it requires the export method to return string or blob.