KA
kahooadil
Blocked user by Spotware at 20 Feb 2023, 10:42
0 follower(s) 0 following 1 subscription(s)

Information

Username: kahooadil
Member since: 20 Dec 2021
Last login: 20 Dec 2021
Status: Blocked

Activity

Where Created Comments
Algorithms 0 1
Forum Topics 0 0
Jobs 0 0

Last Algorithm Comments

KA
kahooadil · 2 years ago
  1. public static class OperationsUtlity  
  2.      {  
  3.         public static DataTable createDataTable()  
  4.         {  
  5.             DataTable table = new DataTable();     
  6.                 //columns  
  7.             table.Columns.Add("ID", typeof(int));     
  8.             table.Columns.Add("NAME", typeof(string));     
  9.             table.Columns.Add("CITY", typeof(string));    
  10.   
  11.                 //data  
  12.             table.Rows.Add(111, "Devesh", "Ghaziabad");     
  13.             table.Rows.Add(222, "ROLI", "KANPUR");     
  14.             table.Rows.Add(102, "ROLI", "MAINPURI");     
  15.             table.Rows.Add(212, "DEVESH", "KANPUR");  
  16.             table.Rows.Add(102, "NIKHIL", "GZB");  
  17.             table.Rows.Add(212, "HIMANSHU", "NOIDa");  
  18.             table.Rows.Add(102, "AVINASH", "NOIDa");  
  19.             table.Rows.Add(212, "BHUPPI", "GZB");  
  20.               
  21.              return table;  
  22.           }  
  23.          
  24.     } 

 

 

Create a static class

  1. public static class  CSVUtlity  { }

Add Extention Method 

  1. public static void ToCSV(this DataTable dtDataTable, string strFilePath){ 
  1. public static void ToCSV(this DataTable dtDataTable, string strFilePath) {  
  2.     StreamWriter sw = new StreamWriter(strFilePath, false);  
  3.     //headers    
  4.     for (int i = 0; i < dtDataTable.Columns.Count; i++) {  
  5.         sw.Write(dtDataTable.Columns[i]);  
  6.         if (i < dtDataTable.Columns.Count - 1) {  
  7.             sw.Write(",");  
  8.         }  
  9.     }  

apkact.com

Convert To csv

  1.     sw.Write(sw.NewLine);  
  2.     foreach(DataRow dr in dtDataTable.Rows) {  
  3.         for (int i = 0; i < dtDataTable.Columns.Count; i++) {  
  4.             if (!Convert.IsDBNull(dr[i])) {  
  5.                 string value = dr[i].ToString();  
  6.                 if (value.Contains(',')) {  
  7.                     value = String.Format("\"{0}\"", value);  
  8.                     sw.Write(value);  
  9.                 } else {  
  10.                     sw.Write(dr[i].ToString());  
  11.                 }  
  12.             }  
  13.             if (i < dtDataTable.Columns.Count - 1) {  
  14.                 sw.Write(",");  
  15.             }  
  16.         }  
  17.         sw.Write(sw.NewLine);  
  18.     }  
  19.     sw.Close();  

 

Export to CSV on button click

  1. private void btnCSV_Click(object sender, EventArgs e) {  
  2.     DataTable dt = OperationsUtlity.createDataTable();  
  3.     string filename = OpenSavefileDialog();  
  4.     dt.ToCSV(filename);