views:

44

answers:

2

Hi all,

I am using the following code to export a datset to excel sheet.

[WebMethod]
    public static void ExporttoExcel()
    {
        DataSet ds;
       productfactory pf=new productfactory();
        ds = pf.getproducts();
        HttpResponse response = HttpContext.Current.Response;

        // first let's clean up the response.object
        response.Clear();
        response.Charset = "";
        response.ContentEncoding = System.Text.Encoding.Default;

        // set the response mime type for excel
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment;filename=\"products.xls\"");

        // create a string writer
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                // instantiate a datagrid
                DataGrid dg = new DataGrid();                    
                dg.DataSource = ds.Tables[0];
                dg.DataBind();
                dg.RenderControl(htw);
                string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\products.xls";                    
                response.Write(sw.ToString());
               // response.End();

            }
        }       
    }

Problem is that its not raising file download and hence no export is taking place.The same code works fine in a normal method.But with the webmethod its not working.

Please someone help.

Thanks.

+1  A: 

I suggest to make an HttpHandler ending in ashx, and place inside him your code that create the excel file.

then call it from your javascript code like that.

document.location.href = "ExporttoExcel.ashx";
Aristos
Thanks a lot.Actually I need to export the datatable and raise a file download asynchronously.But if I am using 'document.location.href' I see a postback occuring.Do you have any idea to avoid it?
kranthi
@kranthi - return false, on your call and on your click button. For example onclick="return function();", and your function must return false.
Aristos
@Aristos-I used the following HTML <input id="Btnexport" type="button" value="export" onclick="return exporttoexcel();" /> and function exporttoexcel() { document.location.href="Exporttoexcel.ashx"; return false; }But I still see the postback occuring...
kranthi
@kranthi You need to give the javascript code, probably on an other question and some is going to help you.
Aristos
+1  A: 

The problem is that WebMethods are not designed to allow you to interact with the Response object (evident in that it wasn't available and you had to use HttpContext.Current.Response to get to it). WebMethods are designed to be blackbox to the user. They will perform and action and/or return a value.

Perhaps you can give us a better idea of what you are trying to accomplish and we can suggest an alternate solution.

sgriffinusa
actually,the reason for using a webmethod here is we wanted to accomplish the export operation asynchronously using Jquery.Can you suggest an alternate to acheive my requirement?
kranthi
If that is the case, I can only echo @Aristos suggestion. Create the HttpHandler as an ashx class. Copy your existing Excel code into the ashx page. You can then use the ASHX page as if it were a URL to the Excel spreadsheet sitting on a server at that location.
sgriffinusa
Sounds like a good idea.I just tried out @Aristos idea and that seems to be working fine.Thanks a lot.
kranthi
I tried @Aristos idea.But I see a postback occuring as I am redirecting to the handler.ashx page.Is there a way to avoid it?
kranthi