111,120
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 读入一个pds导出的管线文件,通过参数返回
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <param name="data">数据</param>
/// <param name="columns">列表栏位</param>
/// <param name="Msg">消息,成功读取时为空,由于考虑需要一次对整个文件进行检验,所以没有直接抛出异常,而使用msg返回</param>
/// <returns>bool</returns>
public bool PipingFileReader(string FilePath, out List<Dictionary<string, string>> data, out List<string> columns, out string Msg)
{
string strLine;
string strHeader;
string strDataArea;
string[] strArray;
char[] charArray = new char[] {'|'};
int RowCnt = 0;
int cntHeaderColumns;
int cntDataAreaCloums;
int cntDataAreaRows = 0;
//初始返回
data = new List<Dictionary<string, string>>();
columns = new List<string>();
Msg = "";
try
{
FileStream aFile = new FileStream(FilePath, FileMode.Open);
StreamReader sr = new StreamReader(aFile,Encoding.Default);//使用默认编码,防止乱码
// Obtain the columns from the first line
// Split row of data into string array
strLine = sr.ReadLine();
RowCnt++;
//顺序读取直到表头区域
while (strLine != null)
{
if (IsHeaderRow(strLine))
{
break;
}
strLine = sr.ReadLine();
RowCnt++;
}
//对数据表头进行检查
if (CheckTableHeader(strLine))
{
strHeader = TrimHeaderRow(strLine);
strArray = strHeader.Split(charArray);
cntHeaderColumns = strArray.GetUpperBound(0);//GetUpperBound(0) 返回 Array 的第一维的索引上限,GetUpperBound(Rank - 1) 返回 Array 的最后一维的上限。此方法的运算复杂度为 O(1)。
columns.Add("行号");//记录该行数据在源文件中的行号
for (int x = 0; x <= cntHeaderColumns; x++)
{
columns.Add(strArray[x].Trim());
}
}
else
{
Msg = "错误的单线材料表表头\r\n";
return false;
}
strLine = sr.ReadLine();
RowCnt++;
while (strLine != null)
{
if (IsDataRow(strLine))
{//如果遇到数据行
// Split row of data into string array
strDataArea = TrimDataRow(strLine);
strArray = strDataArea.Split(charArray);
Dictionary<string, string> dataRow = new Dictionary<string, string>();
cntDataAreaCloums=strArray.GetUpperBound(0);
if (cntDataAreaCloums != cntHeaderColumns)
{
Msg += "第" + RowCnt + "数据格式错误\r\n";
}
else
{
dataRow.Add("行号", RowCnt.ToString());//记录该行数据在源文件中的行号
for (int x = 0; x <= cntDataAreaCloums; x++)
{ //将索引该为1,0号位已经改为“行号”
dataRow.Add(columns[x + 1], strArray[x].Trim());
//但是此索引任然应该从0开始
}
data.Add(dataRow);
cntDataAreaRows++;
}
}
strLine = sr.ReadLine();
RowCnt++;
}
sr.Close();
if (Msg == "")
{
if (cntDataAreaRows % 2 == 0)
{
return true;
}
else
{
Msg = "数据区异常:没有保存数据区的2行缺省\r\n";
return false;
}
}
else
{
return false;
}
}
catch (IOException ex)
{
Msg += ex.ToString();
return false;
}
}
/// <summary>
/// 对单线转换为datatable的预处理
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Msg">错误消息</param>
/// <returns></returns>
public DataTable PrePipingTable(string Path, out string Msg)
{
List<Dictionary<string, string>> data;
List<string> columns;
DataTable dt = new DataTable();
bool IsSuccess = PipingFileReader(Path, out data, out columns,out Msg);
if (!IsSuccess)
{
return dt;
}
foreach (string column in columns)
{
dt.Columns.Add(column,System.Type.GetType("System.String"));
}
foreach (Dictionary<string, string> row in data)
{
DataRow dr = dt.NewRow();
foreach (string clolumn in columns)
{
dr[clolumn] = row[clolumn];
}
dt.Rows.Add(dr);
}
return dt;
}
string[] temp = System.IO.File.ReadAllLines("E:\\aaa.txt", System.Text.Encoding.GetEncoding("gb2312"));
Dictionary<int, product> dic = new Dictionary<int, product>();
foreach (string s in temp)
{
if (s.StartsWith("abc"))
{
string[] pInfo = s.Split('|');
int id = int.Parse(pInfo[1]);
if (!dic.ContainsKey(id))
{
product p = new product();
p.id = id;
p.name = pInfo[2];
p.count = int.Parse(pInfo[3]);
p.remarks = pInfo[4];
dic.Add(id, p);
}
}
else if (s.StartsWith("aa"))
{
string[] qInfo = s.Split('|');
int id = int.Parse(qInfo[1]);
if (dic.ContainsKey(id))
{
product p = dic[id];
int count = int.Parse(qInfo[2]);
string quality = qInfo[3];
if (quality == "良好商品")
p.goodproductNo += count;
else if (quality == "不良商品")
p.badproductNo += count;
}
}
}
foreach (int i in dic.Keys)
{
product p = dic[i];
Console.WriteLine("Id:{0},name:{1},count:{2},remarks:{3},goodproductNo:{4},badproductNo:{5}", p.id, p.name, p.count, p.remarks, p.goodproductNo, p.badproductNo);
}
/*
输出:
Id:10,name:测试1,count:2,remarks:备注,goodproductNo:2,badproductNo:0
Id:11,name:测试2,count:3,remarks:备注,goodproductNo:2,badproductNo:1
*/
用txt做数据源连接字符串
//text作为数据源的链接字符串
string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test;
Extended Properties=""text;HDR=Yes;FMT=Delimited"";";
注意:c:\\test是个文件夹,里面有相应的txt文档做数据源。
