封装一个CSVHelper

  1     public class CSVHelper
  2     {
  3         /// <summary>
  4         /// CSV转换成DataTable(OleDb数据库访问方式)
  5         /// </summary>
  6         /// <param name="csvPath">csv文件路径</param>
  7         /// <returns></returns>
  8         public static DataTable CSVToDataTableByOledb(string csvPath)
  9         {
 10             DataTable csvdt = new DataTable("csv");
 11             if (!File.Exists(csvPath))
 12             {
 13                 throw new FileNotFoundException("csv文件路径不存在!");
 14             }
 15 
 16             FileInfo fileInfo = new FileInfo(csvPath);
 17             using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileInfo.DirectoryName + ";Extended Properties='Text;'"))
 18             {
 19                 OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + fileInfo.Name + "]", conn);
 20                 adapter.Fill(csvdt);
 21             }
 22             return csvdt;
 23         }
 24 
 25         /// <summary>
 26         /// CSV转换成DataTable(文件流方式)
 27         /// </summary>
 28         /// <param name="csvPath">csv文件路径</param>
 29         /// <returns></returns>
 30         public static DataTable CSVToDataTableByStreamReader(string csvPath)
 31         {
 32             DataTable csvdt = new DataTable("csv");
 33 
 34             int intColCount = 0;
 35             bool blnFlag = true;
 36             DataColumn column;
 37             DataRow row;
 38             string strline = null;
 39             string[] aryline;
 40 
 41             using (StreamReader reader = new StreamReader(csvPath, FileUtil.GetEncoding(csvPath)))
 42             {
 43                 while (!string.IsNullOrEmpty((strline = reader.ReadLine())))
 44                 {
 45                     aryline = strline.Split(new char[] { ',' });
 46 
 47                     if (blnFlag)
 48                     {
 49                         blnFlag = false;
 50                         intColCount = aryline.Length;
 51                         for (int i = 0; i < aryline.Length; i++)
 52                         {
 53                             column = new DataColumn(aryline[i]);
 54                             csvdt.Columns.Add(column);
 55                         }
 56                         continue;
 57                     }
 58 
 59                     row = csvdt.NewRow();
 60                     for (int i = 0; i < intColCount; i++)
 61                     {
 62                         row[i] = aryline[i];
 63                     }
 64                     csvdt.Rows.Add(row);
 65                 }
 66             }
 67 
 68             return csvdt;
 69         }
 70 
 71         /// <summary>
 72         /// DataTable 生成 CSV
 73         /// </summary>
 74         /// <param name="dt">DataTable</param>
 75         /// <param name="csvPath">csv文件路径</param>
 76         public static void DataTableToCSV(DataTable dt, string csvPath)
 77         {
 78             if (null == dt)
 79                 return;
 80 
 81             StringBuilder csvText = new StringBuilder();
 82             StringBuilder csvrowText = new StringBuilder();
 83             foreach (DataColumn dc in dt.Columns)
 84             {
 85                 csvrowText.Append(",");
 86                 csvrowText.Append(dc.ColumnName);
 87             }
 88             csvText.AppendLine(csvrowText.ToString().Substring(1));
 89 
 90             foreach (DataRow dr in dt.Rows)
 91             {
 92                 csvrowText = new StringBuilder();
 93                 foreach (DataColumn dc in dt.Columns)
 94                 {
 95                     csvrowText.Append(",");
 96                     csvrowText.Append(dr[dc.ColumnName].ToString().Replace(',', ' '));
 97                 }
 98                 csvText.AppendLine(csvrowText.ToString().Substring(1));
 99             }
100 
101             File.WriteAllText(csvPath, csvText.ToString(), Encoding.Default);
102         }
103     }
View Code

 

posted on 2018-11-24 19:24  粗狂的_蜗牛  阅读(468)  评论(0编辑  收藏  举报