Praveen Kumar

Praveen Kumar

  • 309
  • 6.1k
  • 1.6m

What is the best approach to Convert DataTable into .CSV

Sep 21 2019 3:57 AM
I have written the following two approach to Convert DataTable into .csv File
 
First Approach 
 
Step 1: 

By iteratiing over DataTable put all Coulmns then after each rows column's data with comma (,) separated into a StringBuilder object
 
Step 2: 
 
Now create a new File1.csv file with StringBuilder object which contains csv file data. 
 
Second Approach

Step 1: 
Now create a new File1.csv file with empty string data.

Step 2:
Now use the file path of File1.csv to create a StreamWriter object (ie. sw) and use sw.Write() method into loop to of DataTable Rows.
Using StringBuilder or StreamWriter, which one is best.
 
If any other may be a best approach, kindly share. 
 

Answers (2)