Datagrid -> Excel?

  • Thread starter Thread starter Rob Petersen
  • Start date Start date
R

Rob Petersen

Can anyone recommend a good, reliable solution or 3rd party component to
send a DataGrid or DataTable's contents to Excel? I've tried the following
code but it does not work reliably with large amounts of data (we have about
50,000 rows).

Server.ScriptTimeout = 1000;

Response.ContentType = "application/vnd.ms-excel";

Response.Charset = "";

EnableViewState = false;

System.IO.StringWriter stringWriter = new System.IO.StringWriter();

HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

DataGrid1.RenderControl(htmlWriter);

Response.Write(stringWriter.ToString());

Response.End();
 
An alternative that I use is to create a comma delimited text file by
looping through the datatable in the datagrid's datasource (no datagrid
needed, really) and using a streamwriter to create the file. Save it
with the suffix .csv to a location on the server and then provide the
user with a link to the new file. It will automatically open in Excel
in the browser.

Bill E.
Hollywood, FL
 
Back
Top