Exporting Datatable to csv file

G

Guest

I am trying to export a datatable to a .csv file from an aspx page. I found
the following code, but would like to save the file on the client side and
not the server which is now happening. I would appreciate any help that can
be offered. Thanks.

// Create the CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter("c:/griddata.txt");
// First we will write the headers.
DataTable dt = ds.Tables[0];
int iColCount = dt.Columns.Count;
for(int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr))
{
sw.Write(dr.ToString());
}
if ( i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top