首页 > 其他 > 详细

读取TXT文件,转成Table

时间:2019-04-15 15:10:03      阅读:143      评论:0      收藏:0      [点我收藏+]
 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

 

读取TXT文件,转成Table

原文:https://www.cnblogs.com/-jwj/p/10710580.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!