private void WriteBytes(FileInfo src, string dest, System.Text.Encoding coding) { CSVTable table = CSVUtil.sington.UtilCsv(src); try { using (FileStream fs = new FileStream(dest, FileMode.Create)) { BinaryWriter write = new BinaryWriter(fs, coding); //先预留一个long记录文件大小 write.Seek(8, SeekOrigin.Begin); write.Write(table.rowCnt); for (int i = 0, max = table.sortlist.Count; i < max; i++) { for (int j = 0, len = table.sortlist[i].row.Length; j < len; j++) { CSVStruct st = table.sortlist[i].row[j]; st.parse.Write(write, st.content); st.parse.title = st.title; } } fs.Seek(0, SeekOrigin.Begin); write.Write(fs.Length); fs.Seek(0, SeekOrigin.End); write.Seek(0, SeekOrigin.Begin); write.Write(fs.Length); write.Flush(); write.Close(); XDebug.Log(dest); } } catch (Exception ex) { XDebug.LogError("解析表格" + table.name + "失败," + ex.Message + " \n" + ex.StackTrace); MessageBox.Show("解析表格" + table.name + "失败," + ex.Message + " \n" + ex.StackTrace, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
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); }