读取TXT文件,转成Table

 1         #region -----テキストファイルの取込む(全データ)-----
 2         /// <summary>
 3         /// テキストファイルの取込む(全データ)
 4         /// </summary>
 5         /// <param name="strfile">ファイルパス</param>
 6         /// <param name="strPath">ファイル名</param>
 7         /// <param name="intBeginRow">取込む開始行</param>
 8         /// <param name="intEndRow">取込む終了行</param>
 9         /// <param name="split">分割符号</param>
10         /// <param name="dtReturn">戻るテーブル</param>
11         public static void TextFileReadAll(string strPath,
12                                         string strfile,
13                                         char[] split,
14                                         bool blLastSplit,
15                                         ref DataTable dtReturn)
16         {
17             try
18             {
19                 //使用中行番号
20                 int intRowCount = 0;
21                 String line;
22                 string[] buffer;
23                 string str;
24                 DataRow dr;
25 
26                 using (StreamReader sr = new StreamReader(strPath + "\\" + strfile, Encoding.GetEncoding("Shift-JIS")))
27                 {
28                     //行を取り込む
29                     while ((line = sr.ReadLine()) != null)
30                     {
31                         //行番号追加
32                         intRowCount++;
33 
34                         str = string.Empty;
35 
36                         //最後の分割符号を消す
37                         if (!blLastSplit)
38                         {
39                             line = line.Remove(line.Length - 1);
40                         }
41 
42                         buffer = line.Split(split);
43                         dr = dtReturn.NewRow();
44 
45                         for (int i = 0; i < buffer.Length; i++)
46                         {
47                             dr[i] = buffer[i].Trim();
48                         }
49 
50                         //行をテーブルに追加する
51                         dtReturn.Rows.Add(dr);
52                     }
53                 }
54             }
55             catch (Exception ex)
56             {
57                 throw ex;
58             }
59         }
60         #endregion

 

posted @ 2019-04-15 14:45  _jwj  阅读(264)  评论(0编辑  收藏  举报