Possible Duplicate:
Efficient Method for Creating CSV String from Lists/SortedLists C#?
Here i need to export database information to the .csv file using c#,
Here i am giving my code, but its taking more time loading to the csv file
public void CSVFile(DataTable table, string FilePath)
{
// Create the CSV file to which grid data will be exported.
StreamWriter swr = new StreamWriter(FilePath, false);
//First we will write the headers.
int iColCount = table.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
swr.Write(dtDataTablesList.Columns[i]);
if (i < iColCount - 1)
{
swr.Write(",");
}
}
swr.Write(swr.NewLine);
// Now write all the rows.
foreach (DataRow dr in table.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
swr.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
swr.Write(",");
}
}
swr.Write(sw.NewLine);
}
swr.Close();
}
Please tell me better way of doing
Thanks
Yes recently i had the same issue,
i have the solution, try this code.
with this code we can export database information to the csv file speedly using datareader.