• 封装一个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
  • 相关阅读:
    导航栏的修改
    [题解](背包)luogu_P4095 eden的新背包问题
    [題解](貪心/堆)luogu_P2107小Z的AK計劃
    [題解](最短路)luogu_P2384最短路
    [題解](單調隊列dp)luogu_P1725琪露諾
    [題解](單調隊列/水)luogu_P3088擠奶牛
    [題解](單調隊列dp)【2016noip福建夏令營】探險
    [題解](水/最短路)出题人大战工作人员
    [题解](最短路)最短路点数
    [題解]luogu_P1613跑路(最短路/倍增)
  • 原文地址:https://www.cnblogs.com/zzlblog/p/10013122.html
Copyright © 2020-2023  润新知