using CnasSynchronousCommon; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; namespace CnasSynchronusDAL { public class TXTDAL { /// /// 获取表名表结构 /// public static DataTable ReadTxtFileColumn(string strFilePath,string strTxtFileMode) { DataTable dt = new DataTable(); try { if (strTxtFileMode == "0" || strTxtFileMode == "1") //采用0号规则或1号规则读取数据 { //根据路径打开文件,并获取文件第一行数据,如果第一行数据是列名,则返回这些列名,如果不是,则返回F1,F2... List arryList = new List(); if (File.Exists(strFilePath)) { FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader _StreamReaderKey = new StreamReader(fileStream, GetType(strFilePath)); string strLine = ""; /*这里只读取第一行数据*/ strLine = _StreamReaderKey.ReadLine(); arryList.Add(strLine); fileStream.Close(); } if (arryList.Count >= 0) { string[] temp = arryList[0].Split('\t'); if (strTxtFileMode == "0") //此时是文本模式1,此时第一行是数据列信息 { for (int i = 0; i < temp.Length; i++) { dt.Columns.Add($"F{i + 1}"); } } else if (strTxtFileMode == "1")//此时是文本模式2,此时第一行不是数据列信息,直接就是数据 { foreach (string item in temp) { dt.Columns.Add(item); } } } } if (strTxtFileMode == "2") //此时是2号规则读取文件数据 { dt = ReadFileSecond.ReadTableStructure(); } if (strTxtFileMode == "3") { dt = ReadFileThird.ReadTableStructure(); } if (strTxtFileMode == "4") { dt = ReadFileForth.ReadTableStructure(); } if (strTxtFileMode == "5") { dt = ReadFileFifth.ReadTableStructure(); } } catch (Exception ex) { AppLog.Error(ex.Message); } return dt; } public static DataTable ReadTxtFileData(string strFilePath, string strTxtFileMode) { DataTable dt = new DataTable(); try { if (strTxtFileMode == "0" || strTxtFileMode == "1") //采用0号规则或1号规则读取数据 { //根据路径打开文件,并获取文件第一行数据,如果第一行数据是列名,则返回这些列名,如果不是,则返回F1,F2... List arryList = new List(); if (File.Exists(strFilePath)) { FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader _StreamReaderKey = new StreamReader(fileStream, GetType(strFilePath)); string strLine = ""; while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine()))) { arryList.Add(strLine); } fileStream.Close(); } if (arryList.Count >= 0) { //1 .构建表结构 string[] temp = arryList[0].Split('\t'); if (strTxtFileMode == "0") //此时是文本模式1,此时第一行是数据列信息 { for (int i = 0; i < temp.Length; i++) { dt.Columns.Add($"F{++i}"); } } else if (strTxtFileMode == "1")//此时是文本模式2,此时第一行不是数据列信息,直接就是数据 { foreach (string item in temp) { dt.Columns.Add(item); } } //插入表数据 int index = 0; for (int j = 0; j < arryList.Count; j++) { if (strTxtFileMode == "1" && j == 0) continue; index = 0; DataRow drNew = dt.NewRow(); temp = arryList[j].Split('\t'); foreach (var item in temp) { drNew[index++] = item; } dt.Rows.Add(drNew); } } } if (strTxtFileMode == "2") //此时是2号规则读取文件数据 { dt = ReadFileSecond.ReadTableData(strFilePath); } if (strTxtFileMode == "3") { dt = ReadFileThird.ReadTableData(strFilePath); } if (strTxtFileMode == "4") { dt = ReadFileForth.ReadTableData(strFilePath); } if (strTxtFileMode == "5") { dt = ReadFileFifth.ReadTableData(strFilePath); } } catch (Exception ex) { AppLog.Error(ex.Message); } return dt; } #region 得到文本的编码格式,避免因解析问题造成的乱码 public static System.Text.Encoding GetType(string FILE_NAME) { FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read); Encoding r = GetType(fs); fs.Close(); return r; } public static System.Text.Encoding GetType(FileStream fs) { byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 }; byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 }; byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM Encoding reVal = Encoding.Default; BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default); int i; int.TryParse(fs.Length.ToString(), out i); byte[] ss = r.ReadBytes(i); if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)) { reVal = Encoding.UTF8; } else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00) { reVal = Encoding.BigEndianUnicode; } else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41) { reVal = Encoding.Unicode; } r.Close(); return reVal; } private static bool IsUTF8Bytes(byte[] data) { int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 byte curByte; //当前分析的字节. for (int i = 0; i < data.Length; i++) { curByte = data[i]; if (charByteCounter == 1) { if (curByte >= 0x80) { //判断当前 while (((curByte <<= 1) & 0x80) != 0) { charByteCounter++; } //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X if (charByteCounter == 1 || charByteCounter > 6) { return false; } } } else { //若是UTF-8 此时第一位必须为1 if ((curByte & 0xC0) != 0x80) { return false; } charByteCounter--; } } if (charByteCounter > 1) { throw new Exception("非预期的byte格式"); } return true; } #endregion } }