|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using CnasSynchronousCommon;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Text;
-
- namespace CnasSynchronusDAL
- {
- public class ReadFileCommon01 : BaseReadFileMode
- {
- /// <summary>
- /// 分割每个元素的依据。0代表“ ”,1代表“\t”,2代表0和1的集合,3代表逗号
- /// </summary>
- public static string SplitType { get; set; }
-
- /// <summary>
- /// 起始行数(部分txt文件中,表数据的起始位置不是第一行,而是从其他行开始)
- /// </summary>
- public static int StartLineIndex { get; set; }
-
- /// <summary>
- /// 是否自定义创建日期字段
- /// </summary>
- public static bool IfCustomCreateDateColumn { get; set; }
-
- /// <summary>
- /// 后缀名类似于(.txt)
- /// </summary>
- public static string Suffix { get; set; }
-
- /// <summary>
- /// 以日期命名文件名称时的日期格式(yyyyMMdd)
- /// </summary>
- public static string FileNameFormat { get; set; }
-
- public static DataTable ReadTableStructure(string strFilePath)
- {
- DataTable dt = new DataTable();
- try
- {
- List<string> arryList = ReadFileMessage(strFilePath);
-
- if (arryList.Count >= StartLineIndex)
- {
- string[] strRowDatas = arryList[StartLineIndex].Trim().Split(GetSplitStrings(), StringSplitOptions.RemoveEmptyEntries);
- if (strRowDatas.Length > 0)
- {
- for (int i = 0; i < strRowDatas.Length; i++)
- {
- dt.Columns.Add($"F{i + 1}");
- }
-
- if (IfCustomCreateDateColumn)
- dt.Columns.Add("日期(系统添加)");
- }
- }
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- }
- return dt;
- }
-
- internal static DataTable ReadTableData(string strFilePath)
- {
- DataTable dt = new DataTable();
- try
- {
- List<string> arryList = ReadFileMessage(strFilePath);
-
- if (arryList.Count >= StartLineIndex)
- {
- string[] strRowDatas = arryList[StartLineIndex].Trim().Split(GetSplitStrings(), StringSplitOptions.RemoveEmptyEntries);
- if (strRowDatas.Length > 0)
- {
- for (int i = 0; i < strRowDatas.Length; i++)
- {
- dt.Columns.Add($"F{i + 1}");
- }
-
- if (IfCustomCreateDateColumn)
- dt.Columns.Add("日期(系统添加)");
- }
- }
-
- if (arryList.Count > 0)
- {
- for (int i = 0; i < arryList.Count; i++)
- {
- if (i+1<StartLineIndex) continue; //不是数据行
-
- string strData = arryList[i].Trim();
- string[] strRowDatas = strData.Split(GetSplitStrings(), StringSplitOptions.RemoveEmptyEntries);
- if (strRowDatas.Length >0)
- {
- DataRow dr = dt.NewRow();
-
- for (int j = 0; j < strRowDatas.Length; j++)
- {
- dr[$"F{j + 1}"] = strRowDatas[j].Trim();
- }
-
- if (IfCustomCreateDateColumn)
- {
- if (FileNameFormat.Length > 0&&Suffix.Length>0)
- {
- int subLength = (Suffix.Length - 1) * 2 + 1 + FileNameFormat.Length;
- DateTime dtTime = DateTime.ParseExact(string.Concat(strFilePath.Substring(strFilePath.Length - subLength, subLength).Take(FileNameFormat.Length)), FileNameFormat, new System.Globalization.CultureInfo("zh-CN", true));
- dr["日期(系统添加)"] = dtTime.ToString(FileNameFormat);
- }
- }
-
- dt.Rows.Add(dr);
- }
- }
- }
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- }
- return dt;
- }
-
- private static List<string> ReadFileMessage(string strFilePath)
- {
- List<string> arryList = new List<string>();
- 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();
- }
- return arryList;
- }
-
- private static string[] GetSplitStrings()
- {
- string[] splitStrings =null;
- switch (SplitType)
- {
- case "0":
- splitStrings = new string[] { " "};
- break;
- case "1":
- splitStrings = new string[] { "\t" };
- break;
- case "2":
- splitStrings = new string[] { " ","\t" };
- break;
- case "3":
- splitStrings = new string[] { "," };
- break;
- }
- return splitStrings;
- }
- }
- }
|