export data from WCF Service to excel
- by Dave
I need to provide an export to excel feature for a large amount of data returned from a WCF web service.
The code to load the datalist is as below:
List<resultSet> r = myObject.ReturnResultSet(myWebRequestUrl); //call to WCF service
myDataList.DataSource = r;
myDataList.DataBind();
I am using the Reponse object to do the job:
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=MyExcel.xls");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter tw = new HtmlTextWriter(sw);
myDataList.RenderControl(tw);
Response.Write(sb.ToString());
Response.End();
The problem is that WCF Service times out for large amount of data (about 5000 rows) and the result set is null. When I debug the service, I can see the window for saving/opening the excel sheet appear before the service returns the result and hence the excel sheet is always empty. Please help me figure this out.