有一文本文件内容如下:           地区          编号         金额      应交金额 
          上海        01001            22          108
                      01003            41          146
                      01004             1           10
                      01005            13           36
                      01007            64          334
-------------------------------------------------------
      地区小计        -----        14465        104660
-------------------------------------------------------
          南京        02001            22          108
                      02003            41          146
                      02004            1            10
                      02005            13           36
                      02007            64          334
-------------------------------------------------------
      地区小计        -----        14465        104660
-------------------------------------------------------
我需要从里面将各列数据读出来导入数据库。地区列可不导。我用以下方法:     private void txtload() 
    { 
        string Path_DC="d:\\ee.txt"; 
        FileStream fs = new FileStream(Path_DC, FileMode.Open, FileAccess.Read); 
        StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default); 
        string tempstr = null; 
        int count = 0; 
        while ((tempstr = sr.ReadLine()) != null) 
          { 
                count++; 
                if(count >=2 ) //从第二行开始读取
                { 
            Response.Write(tempstr.Length); 
            string[] a = new string[5];             a[0] = (tempstr.Substring(21, 5)); 
            a[1] = (tempstr.Substring(28, 14)).Trim(); 
            a[2] = (tempstr.Substring(42, 14)).Trim(); 
            a[3] = (tempstr.Substring(56, 14)).Trim(); 
            a[4] = (tempstr.Substring(70, 14)).Trim();             Response.Write(a[0]); 
            Response.Write(a[1]); 
            Response.Write(a[2]); 
            Response.Write(a[3]); 
            Response.Write(a[4]); 
            Response.Write(" <br>");  
        
                } 
            } 
          }   问题是带“上海”的这一行,本以为各行字符长度一样,但事实是:编号列字符01001是从第21个字符开始,01003行的字符是从第23个开始,还有小计上面和下面的-------------线不知该如何处理。请问如何能将每行每列数据读出?请高手解答, 谢谢!! 注意,每列是对齐的

解决方案 »

  1.   

    string[] item= strRow.Split();
      

  2.   

    string[] item= strRow.Split(); 
    他们之间的分隔符不确定, 不能用这个方法,而且有汉字的那行,不好处理啊。
      

  3.   

    string[] arr=File.ReadAllLine("");
    foreach (string s in arr) 

    string[] arr2= s.Split(new char[] {''}, StringSplitOptions.RemoveEmptyEntries); 
     } 
    或使用txt作为数据源
      

  4.   

    能详细解释下吗?什么意思?
    string[] arr2= s.Split(new char[] {''}, StringSplitOptions.RemoveEmptyEntries); 
      

  5.   

    以String.Empty 分割,并移除空实体
    比如 "A  B" 中间有两个空格,那么用 String arryStr = "A  B".Split(string.empty);
    此时 arryStr.Count == 3,其中有一个 空白字符,使用 StringSplitOptions.RemoveEmptyEntries
    就是将其移除
      

  6.   

    知道楼住的意思了,那你就全按byte来读            FileStream fs = new FileStream(Path_DC, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
                string tempstr = null;
                int count = 0;
                while ((tempstr = sr.ReadLine()) != null)
                {
                    byte[] rowBytes = System.Text.Encoding.Default.GetBytes(tempstr);                count++;
                    if (count >= 2) //从第二行开始读取 
                    {
                        string[] a = new string[5];                    a[0] = System.Text.Encoding.Default.GetString(rowBytes, 21, 5);
                        a[1] =  System.Text.Encoding.Default.GetString(rowBytes, 28, 14);
                        //....
                        
                        //a[0] = (tempstr.Substring(21, 5));
                        //a[1] = (tempstr.Substring(28, 14)).Trim();
                        //a[2] = (tempstr.Substring(42, 14)).Trim();
                        //a[3] = (tempstr.Substring(56, 14)).Trim();
                        //a[4] = (tempstr.Substring(70, 14)).Trim();
                    }
                }
      

  7.   

    用正则表达式处理,简单些。
                System.Text.RegularExpressions.Regex reT 
                    = new System.Text.RegularExpressions.Regex(@"(?<code>\d+) +(?<sum>\d+) +(?<shouldpay>\d+) *(?=\r\n)");
                System.Text.RegularExpressions.MatchCollection mcT = reT.Matches(textBox1.Text);//textBox1.Text替换下
                for (int I = 0; I < mcT.Count; I++)
                    System.Console.WriteLine(mcT[I].Groups["code"].Value + ", " + mcT[I].Groups["sum"].Value + ", " + mcT[I].Groups["shouldpay"].Value);
      

  8.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamReader sr = new StreamReader(@"d:\1.txt", Encoding.Default);
                string[] arryStr = null;
                string 编号 = string.Empty;
                string 金额 = string.Empty;
                string 应交金额 = string.Empty;
                int i = 0;            while (sr.Peek() >= 0)
                {
                    i++;
                    if (i == 1) break;
                    arryStr = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (arryStr.Length == 4)
                    {
                        if (arryStr[0].Equals("地区小计")) continue;
                        编号 = arryStr[1];
                        金额 = arryStr[2];
                        应交金额 = arryStr[3];
                    }
                    else if (arryStr.Length == 3)
                    {
                        编号 = arryStr[0];
                        金额 = arryStr[1];
                        应交金额 = arryStr[2];
                    }
                    //插入数据库
                    //insert into table xxx(编号,金额,应交金额) values(编号,金额,应交金额)
                }
            }
        }
    }
      

  9.   

    13楼的,我明白什么意思了,但不知怎么我运行不了,得不到数组的返回值:        while (sr.Peek() >= 0)
            {
                i++;
                if (i >= 6) //从第6行开始读取
                {
                    arryStr = sr.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                         Response.Write(arryStr[0]);一运行就提示 索引超出了数组界限, 不知为什么,arryStr[0]也会超吗?