public CSVTable UtilCsv(FileInfo file) { //针对ANSI编码的csv 不要用Unicode编码 StreamReader sr = new StreamReader(file.FullName, Encoding.Default); CSVTable table = new CSVTable(file.Name); string attachmsg = string.Empty; bool isSort = true; //字段 string tile = sr.ReadLine(); if (string.IsNullOrEmpty(tile.TrimEnd(eof))) { attachmsg = "标题为null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } //注释 string comment = sr.ReadLine(); if (string.IsNullOrEmpty(comment.TrimEnd(eof))) { attachmsg = "注释为null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } //client使用还是server使用 string mode = sr.ReadLine(); if (string.IsNullOrEmpty(mode.TrimEnd(eof))) { attachmsg = "读表地方null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } string[] modes = UtilLine(mode); bool[] useList = new bool[modes.Length]; int useColumeCnt = 0; for (int i = 0; i < modes.Length; i++) { useList[i] = modes[i].Equals("A") || modes[i].Equals("C"); if (useList[i]) { useColumeCnt++; } } //类型 string tp = sr.ReadLine(); if (string.IsNullOrEmpty(tp.TrimEnd(eof))) { attachmsg = "类型为null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } string[] titles = RemoveUnuseless(UtilLine(tile.TrimEnd(eof)), useList); if (titles.Length > 0) { isSort &= titles[0].Contains("ID"); } string[] comments = RemoveUnuseless(UtilLine(comment), useList); string[] tps = RemoveUnuseless(UtilLine(tp), useList); ValueParse[] parses = new ValueParse[tps.Length]; for (int i = 0, max = parses.Length; i < max; i++) { parses[i] = TransParse(tps[i], file.Name); } if (tps.Length > 0) { isSort &= (tps[0].Equals("int") || tps[0].Equals("uint")); } int lineCnt = 0; table.sortlist = new List <CVSSortRow>(); while (true) { string line = sr.ReadLine(); if (string.IsNullOrEmpty(line)) { break; } string[] colums = RemoveUnuseless(UtilLine(line), useList); if (colums == null || tp == null) { attachmsg = "内容有null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } else if (colums.Length != tps.Length || colums.Length != titles.Length) { attachmsg = "字段不等长 内容:" + colums.Length + " tpye:" + tps.Length + "\n" + line; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } CVSSortRow sortRow = new CVSSortRow(); sortRow.row = new CSVStruct[useColumeCnt]; for (int i = 0, max = colums.Length; i < max; i++) { CSVStruct sct = new CSVStruct(); sct.title = titles[i]; sct.comment = comments[i]; sct.parse = TransParse(tps[i], file.Name); sct.content = string.Intern(colums[i]); if (i == 0) { sortRow.sortid = isSort ? int.Parse(colums[i]) : 0; } sortRow.row[i] = sct; } table.sortlist.Add(sortRow); lineCnt++; } table.isSort = isSort; table.rowCnt = lineCnt; table.colCnt = titles.Length; table.titles = titles; table.comments = comments; table.types = tps; table.parses = parses; if (isSort) { table.sortlist.Sort(table.Sort); } return(table); }
public int Sort(CVSSortRow row1, CVSSortRow row2) { return(row1.sortid - row2.sortid); }