a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#中csv文件與DataTable互相導入處理實例解析

2020-01-24 02:36:08
字體:
來源:轉載
供稿:網友

本文介紹了C#中csv文件與DataTable互相導入處理實例解析,主要功能代碼封裝處理下,相對比較簡單。以后項目用到的話可以直接使用。具體方法如下:

1.封裝好的類如下:

using System;using System.Data;using System.IO;using System.Text;using CSharpUtilHelpV2;using StringUtilHelp;namespace DBUtilHelpV2Plus{  public static class DBToolV2Plus  {    /// <summary>    /// 將DataTable導出到CSV.    /// </summary>    /// <param name="table">DataTable</param>    /// <param name="fullSavePath">保存路徑</param>    /// <param name="tableheader">標題信息</param>    /// <param name="columname">列名稱『eg:姓名,年齡』</param>    /// <returns>導出成功true;導出失敗false</returns>    public static bool ToCSV(this DataTable table, string fullSavePath, string tableheader, string columname)    {      ArgumentChecked(table, fullSavePath);      //------------------------------------------------------------------------------------      try      {        string _bufferLine = "";        using (StreamWriter _writerObj = new StreamWriter(fullSavePath, false, Encoding.UTF8))        {          if (!string.IsNullOrEmpty(tableheader))            _writerObj.WriteLine(tableheader);          if (!string.IsNullOrEmpty(columname))            _writerObj.WriteLine(columname);          for (int i = 0; i < table.Rows.Count; i++)          {            _bufferLine = "";            for (int j = 0; j < table.Columns.Count; j++)            {              if (j > 0)                _bufferLine += ",";              _bufferLine += table.Rows[i][j].ToString();            }            _writerObj.WriteLine(_bufferLine);          }          return true;        }      }      catch (Exception)      {        return false;      }    }    /// <summary>    /// 參數檢查    /// </summary>    /// <param name="table"></param>    /// <param name="fullSavePath"></param>    private static void ArgumentChecked(DataTable table, string fullSavePath)    {      if (table == null)        throw new ArgumentNullException("table");      if (string.IsNullOrEmpty(fullSavePath))        throw new ArgumentNullException("fullSavePath");      string _fileName = CSharpToolV2.GetFileNameOnly(fullSavePath);      if (string.IsNullOrEmpty(_fileName))        throw new ArgumentException(string.Format("參數fullSavePath的值{0},不是正確的文件路徑!", fullSavePath));      if (!_fileName.InvalidFileNameChars())        throw new ArgumentException(string.Format("參數fullSavePath的值{0},包含非法字符!", fullSavePath));    }    /// <summary>    /// 將CSV文件數據導入到Datable中    /// </summary>    /// <param name="table"></param>    /// <param name="filePath">DataTable</param>    /// <param name="rowIndex">保存路徑</param>    /// <returns>Datable</returns>    public static DataTable AppendCSVRecord(this DataTable table, string filePath, int rowIndex)    {      ArgumentChecked(table, filePath);      if (rowIndex < 0)        throw new ArgumentException("rowIndex");      using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8, false))      {        int i = 0, j = 0;        reader.Peek();        while (reader.Peek() > 0)        {          j = j + 1;          string _line = reader.ReadLine();          if (j >= rowIndex + 1)          {            string[] _split = _line.Split(',');            DataRow _row = table.NewRow();            for (i = 0; i < _split.Length; i++)            {              _row[i] = _split[i];            }            table.Rows.Add(_row);          }        }        return table;      }    }  }}

2.代碼使用測試如下:

using System;using System.Data;using DBUtilHelpV2;using DBUtilHelpV2Plus;namespace DBUtilHelpV2PlusTest{  class Program  {    static DataTable testDb = null;    static string fullSavePath = string.Format(@"C:/{0}.csv", DateTime.Now.ToString("yyyyMMddHH"));    static void Main(string[] args)    {      try      {        CreateTestDb();        Console.WriteLine(string.Format("DataTable導出到CSV文件{0}.", testDb.ToCSV(fullSavePath, "姓名,年齡", "人員信息表") == true ? "成功" : "失敗"));        testDb.Rows.Clear();        Console.WriteLine(string.Format("清空數據,當前{0}條數據.", testDb.Rows.Count));        testDb = testDb.AppendCSVRecord(fullSavePath, 2);        Console.WriteLine(string.Format("CSV文件導入到Datable,導入{0}條數據.", testDb.Rows.Count));      }      catch (Exception ex)      {        Console.WriteLine(ex.Message);      }      finally      {        Console.ReadLine();      }    }    static void CreateTestDb()    {      if (testDb == null)      {        testDb = DBToolV2.CreateTable("Name,Age|int");        for (int i = 1; i <= 10; i++)        {          DataRow _row = testDb.NewRow();          _row["Name"] = string.Format("YanZhiwei_{0}", i);          _row["Age"] = i;          testDb.Rows.Add(_row);        }      }    }  }}

運行效果如下圖所示:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 在线日韩| 国产精品国产三级国产有无不卡 | 日本在线视频不卡 | 涩涩av| 国产视频福利在线观看 | 国产女人高潮大叫a毛片 | 6080夜射猫 | 人人射人人爱 | 国产精品99久久久久久动医院 | 欧美一区二区三区四区视频 | 国产毛片a级 | av三级在线观看 | 中文字幕日韩欧美一区二区三区 | 久久久中文字幕 | 美女中文字幕视频 | 成人在线播放器 | 日本久久精品视频 | 国产区在线| 欧美爱爱视频 | 色噜噜亚洲 | 亚洲六月丁香色婷婷综合久久 | 久久99国产精一区二区三区 | 91porn在线 | 中文字幕av高清 | 欧美午夜精品一区二区三区 | 亚洲婷婷综合网 | 四虎成人网 | 一级欧美一级日韩片 | 国产美女在线精品免费观看 | 久久亚洲国产精品日日av夜夜 | 日韩色av| 欧美日韩一区二区在线 | 久久国产欧美日韩精品 | 精品毛片 | 91精品国产一区二区三区蜜臀 | 日韩精品一区二区三区 | 美女视频一区二区三区 | 国产精品一品二区三区的使用体验 | 久草青青 | 成人精品在线 | 蜜桃视频麻豆女神沈芯语免费观看 |