欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中csv文件與DataTable互相導(dǎo)入處理實例解析

 更新時間:2014年08月04日 17:36:30   投稿:shichen2014  
這篇文章主要介紹了C#中csv文件與DataTable互相導(dǎo)入處理實例解析,非常實用的功能,需要的朋友可以參考下

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

1.封裝好的類如下:

using System;
using System.Data;
using System.IO;
using System.Text;
using CSharpUtilHelpV2;
using StringUtilHelp;

namespace DBUtilHelpV2Plus
{
  public static class DBToolV2Plus
  {
    /// <summary>
    /// 將DataTable導(dǎo)出到CSV.
    /// </summary>
    /// <param name="table">DataTable</param>
    /// <param name="fullSavePath">保存路徑</param>
    /// <param name="tableheader">標(biāo)題信息</param>
    /// <param name="columname">列名稱『eg:姓名,年齡』</param>
    /// <returns>導(dǎo)出成功true;導(dǎo)出失敗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>
    /// 參數(shù)檢查
    /// </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("參數(shù)fullSavePath的值{0},不是正確的文件路徑!", fullSavePath));
      if (!_fileName.InvalidFileNameChars())
        throw new ArgumentException(string.Format("參數(shù)fullSavePath的值{0},包含非法字符!", fullSavePath));
    }
    /// <summary>
    /// 將CSV文件數(shù)據(jù)導(dǎo)入到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導(dǎo)出到CSV文件{0}.", testDb.ToCSV(fullSavePath, "姓名,年齡", "人員信息表") == true ? "成功" : "失敗"));
        testDb.Rows.Clear();
        Console.WriteLine(string.Format("清空數(shù)據(jù),當(dāng)前{0}條數(shù)據(jù).", testDb.Rows.Count));
        testDb = testDb.AppendCSVRecord(fullSavePath, 2);
        Console.WriteLine(string.Format("CSV文件導(dǎo)入到Datable,導(dǎo)入{0}條數(shù)據(jù).", 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);
        }
      }
    }
  }
}

運行效果如下圖所示:

相關(guān)文章

  • C#的TimeSpan案例詳解

    C#的TimeSpan案例詳解

    這篇文章主要介紹了C#的TimeSpan案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C#常見算法面試題小結(jié)

    C#常見算法面試題小結(jié)

    這篇文章主要介紹了C#常見算法面試題,包含了常見的排序、字符串操作、類的操作等技巧,需要的朋友可以參考下
    2014-09-09
  • C#實現(xiàn)遠程連接ORACLE數(shù)據(jù)庫的方法

    C#實現(xiàn)遠程連接ORACLE數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#實現(xiàn)遠程連接ORACLE數(shù)據(jù)庫的方法,通過自定義函數(shù)db_connection_test實現(xiàn)遠程連接Oracle數(shù)據(jù)庫的功能,是非常實用的技巧,需要的朋友可以參考下
    2014-12-12
  • C#實現(xiàn)簡易猜數(shù)字游戲

    C#實現(xiàn)簡易猜數(shù)字游戲

    這篇文章主要為大家詳細介紹了C#實現(xiàn)簡易猜數(shù)字游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • C#實現(xiàn)ComboBox自動匹配字符

    C#實現(xiàn)ComboBox自動匹配字符

    本文介紹C#如何實現(xiàn)ComboBox自動匹配字符1.采用CustomSource當(dāng)做提示集合2. 直接使用下拉列表中的項作為匹配的集合,需要了解的朋友可以參考下
    2012-12-12
  • C#實現(xiàn)Array添加擴展實例

    C#實現(xiàn)Array添加擴展實例

    這篇文章主要介紹了C#實現(xiàn)Array添加擴展,對C#初學(xué)者有不錯的參考價值,需要的朋友可以參考下
    2014-08-08
  • 淺談C#9.0新特性之參數(shù)非空檢查簡化

    淺談C#9.0新特性之參數(shù)非空檢查簡化

    這篇文章主要介紹了淺談C#9.0新特性之參數(shù)非空檢查簡化,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • .net實現(xiàn)裁剪網(wǎng)站上傳圖片的方法

    .net實現(xiàn)裁剪網(wǎng)站上傳圖片的方法

    這篇文章主要介紹了.net實現(xiàn)裁剪網(wǎng)站上傳圖片的方法,比較實用的功能,需要的朋友可以參考下
    2014-07-07
  • C# 匿名方法基礎(chǔ)回顧

    C# 匿名方法基礎(chǔ)回顧

    本篇文章主要介紹了C#的匿名方法的參數(shù)使用范圍以及委托示例。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Unity實現(xiàn)俄羅斯方塊(三)

    Unity實現(xiàn)俄羅斯方塊(三)

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)俄羅斯方塊的第一部分代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06

最新評論