讀取數據報文的內容,以下為需要讀取的文本數據示例。
庫存管理-庫存事務歷史
事務ID 事務日期 商品編碼
218478806 2011-04-14 2100325K0094
218478808 2011-04-14 2100325K0150
218478810 2011-04-14 2100325K0145
218478812 2011-04-14 2100325K0131
218478814 2011-04-14 2100328K0066
文本是以‘ ’為分隔符。
FrmReaderFile.cs代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace sms
{
public partial class FrmReaderFile : Form
{
public FrmReaderFile()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
//以下用循環產生列?
dt.Columns.Add("事物ID");
dt.Columns.Add("事物日期");
dt.Columns.Add("商品編碼");
dt.Columns.Add("4");
dt.Columns.Add("5");
dt.Columns.Add("6");
string strFilePath = @"E:/數據報文.txt";
if (!File.Exists(strFilePath))
{
MessageBox.Show("未找到文本文件!","提示",MessageBoxButtons.OK,MessageBoxIcon .Error);
return;
}
else
{
FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));
string strLine = sr.ReadLine();
int count =0;
while (strLine != null)
{
string[] strArry = strLine.Split( );
DataRow dr = dt.NewRow();
for (int i = 0; i <= strArry.Length - 1; i++)
{
dr[i] = strArry[i];
}
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
sr.Close();
fs.Close();
gridControl1.DataSource = dt;
}
}
}
示例運行效果:

問題1,我只要讀取第三行以後額數據並顯示,如何?
問題2,怎樣將第二行的數據讀取,並作為列名?
正在進行修改,優化。如果有什麼好的建議,留言與我:f35j10@163.com,謝謝。