有一文本文件内容如下: 地区 编号 金额 应交金额
上海 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个开始,还有小计上面和下面的-------------线不知该如何处理。请问如何能将每行每列数据读出?请高手解答, 谢谢!! 注意,每列是对齐的
上海 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个开始,还有小计上面和下面的-------------线不知该如何处理。请问如何能将每行每列数据读出?请高手解答, 谢谢!! 注意,每列是对齐的
解决方案 »
- 身份证阅读器问题?syn_readMsg 内存不能
- 发生了 System.Threading.ThreadStateException怎么改正?
- 紧急求助!关于收费会员制网站的登录控制问题。
- c# 我写的一个服务,利用静态变量,怎么每次都是从0开始的,而不能保存值
- c#可以做这样的东西吗?类似网管软件的东西
- 网页可以局部刷新吗?
- 重新登入功能如何做?
- 两个字符串相加
- (60分悬赏)我写的程序在每次刷新后,内存占用量都增加104K,这是为什么?
- Win98下 Winform程序连不上access数据库,报错:外部数据库驱动程序中(10498)的意外错误
- 用哪种静态形式比较好(静态成员+直接初始化,静态成员+静态构造函数,静态成员+实例属性初始化)
- 各位大哥 谁能提供 C# 加壳程序源码
他们之间的分隔符不确定, 不能用这个方法,而且有汉字的那行,不好处理啊。
foreach (string s in arr)
{
string[] arr2= s.Split(new char[] {''}, StringSplitOptions.RemoveEmptyEntries);
}
或使用txt作为数据源
string[] arr2= s.Split(new char[] {''}, StringSplitOptions.RemoveEmptyEntries);
比如 "A B" 中间有两个空格,那么用 String arryStr = "A B".Split(string.empty);
此时 arryStr.Count == 3,其中有一个 空白字符,使用 StringSplitOptions.RemoveEmptyEntries
就是将其移除
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();
}
}
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);
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(编号,金额,应交金额)
}
}
}
}
{
i++;
if (i >= 6) //从第6行开始读取
{
arryStr = sr.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
Response.Write(arryStr[0]);一运行就提示 索引超出了数组界限, 不知为什么,arryStr[0]也会超吗?