<p>We let users create ad-hoc queries in our website.  We would like to have the user select their criteria, then click submit and have the results streamed automatically to Excel.  I have the application populating a DataTable, then using the datatable to create a tab delimited string.  The problem is getting that to excel.</p>

<p>What is the best way to stream data to Excel?  Preferrably, we wouldn't have to make users close an empty window after clicking the submit button.</p>

Change the page's file type to excel, and only stream the HTML necessary to build a table to the page. code from
here
```
```
//for demo purpose, lets create a small datatable & populate it with dummy data
System.Data.DataTable workTable = new System.Data.DataTable();

//The tablename specified here will be set as the worksheet name of the generated Excel file. 
workTable.TableName = "Customers";
workTable.Columns.Add("Id");
workTable.Columns.Add("Name");
System.Data.DataRow workRow;

for (int i = 0; i <= 9; i++)
{
workRow = workTable.NewRow();
workRow[0] = i;
workRow[1] = "CustName" + i.ToString();
workTable.Rows.Add(workRow);
}

//...and lets put DataTable2ExcelString to work
string strBody = DataTable2ExcelString(workTable);

Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
Response.AppendHeader("Content-disposition", "attachment; filename=my.xls");
Response.Write(strBody);
```
```