• TXT 与 DataTable 互转


        //*********************************************************************************************
        public static DataTable TXT_To_DataTable(string dirTXT)
        {
            StreamReader objReader = new StreamReader(dirTXT, Encoding.Default);
            DataTable dt = new DataTable();
            string strline;
            string[] aryline;
            bool blnFlag = false;
            while ((strline = objReader.ReadLine()) != null)
            {
                aryline = strline.Split(new char[] { '	' }, StringSplitOptions.None);
                if (blnFlag == false)
                {
                    DataColumn mydc = null;
                    foreach (string str in aryline)
                    {
                        mydc = new DataColumn(str);
                        dt.Columns.Add(mydc);
                    }
                    blnFlag = true;
                }
                else
                {
                    dt.Rows.Add(aryline);
                }
            }
    
            objReader.Close();
            return dt;
        }
        /// <summary>
        /// 以Tab键为间隔,以Tab键表示空值
        /// </summary>
        /// <param name="dirTXT"></param>
        /// <returns></returns>
        public static DataTable TXT_To_DataTable2(string dirTXT)
        {
            StreamReader objReader = new StreamReader(dirTXT, Encoding.Default);
            DataTable dt = new DataTable();
            string strline;
            string[] aryline;
            bool blnFlag = false;
            while ((strline = objReader.ReadLine()) != null)
            {
                aryline = strline.Split(new char[] { '	' }, StringSplitOptions.None);
                if (blnFlag == false)
                {
                    foreach (string str in aryline)
                    {
                        if (!string.IsNullOrEmpty(str))
                        {
                            DataColumn mydc = new DataColumn(str);
                            dt.Columns.Add(mydc);
                        }
                    }
                    blnFlag = true;
                }
                else
                {
                    if (aryline.Count() != dt.Columns.Count)
                    {
                        strline = Lines_To_Replace(strline);
                        aryline = strline.Split(new char[] { '	' }, StringSplitOptions.None);
                    }
                    dt.Rows.Add(aryline);
                }
            }
            objReader.Close();
            return dt;
        }
        public static string Lines_To_Replace(string lines)
        {
            for (int i = 0; i < lines.Length; i++)
            {
                if (i != lines.Length - 1 &&
                    i > 0 &&
                    lines.Substring(i - 1, 1) == "	" &&
                    lines.Substring(i, 1) == "	" &&
                    lines.Substring(i + 1, 1) == "	")
                {
                    //lines.Substring(i, 1) = "";
                    lines = lines.Substring(0, i) + "" + lines.Substring(i + 1, lines.Length - i - 1);
                }
            }
            return lines;
        }
        /// <summary>
        /// 以空格键为间隔,没有空值
        /// </summary>
        /// <param name="dirTXT"></param>
        /// <returns></returns>
        public static DataTable TXT_To_DataTable3(string dirTXT)
        {
            StreamReader objReader = new StreamReader(dirTXT, Encoding.Default);
            DataTable dt = new DataTable();
            string strline;
            bool OK = false;
            while ((strline = objReader.ReadLine()) != null)
            {
                List<string> list = new List<string>();
                string tmp = "";
                while (strline.Contains(" "))
                {
                    tmp = strline.Substring(0, strline.IndexOf(" "));
                    strline = strline.Substring(strline.IndexOf(" ") + 1, strline.Length - strline.IndexOf(" ") - 1).Trim();
                    if (!string.IsNullOrEmpty(tmp))
                        list.Add(tmp);
                }
                if (!string.IsNullOrEmpty(strline))
                    list.Add(strline);
                if (!OK)
                {
                    foreach (string str in list)
                    {
                        if (!string.IsNullOrEmpty(str))
                        {
                            DataColumn mydc = new DataColumn(str);
                            dt.Columns.Add(mydc);
                        }
                    }
                    OK = true;
                }
                else
                dt.Rows.Add(list.ToArray());
            }
            objReader.Close();
            return dt;
        }
    
        /// <summary>
        /// DataTable导出到TXT 以Tab为分隔符,以Tab表示空值
        /// </summary>
        /// <param name="table"></param>
        /// <param name="fullName"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public static bool DataTable_To_TXT2(DataTable table, string fullName, ref string Msg)
        {
            PathHelper.Check_Directory(fullName);
            if (table == null || table.Rows.Count == 0)
            {
                Msg += fullName + " 对应的 DataTable 没有数据";
                return false;
            }
    
            FileStream fileStream = new FileStream(fullName, FileMode.Create);
            StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
            StringBuilder strBuilder = new StringBuilder();
    
            for (int j = 0; j < table.Columns.Count; j++)
            {
                string str1 = table.Columns[j].Caption;
    
                if (j == table.Columns.Count - 1)
                {
                    strBuilder.AppendLine(str1);
                }
                else
                {
                    str1 += "	";
                    strBuilder.Append(str1);
                }
            }
    
            for (int i = 0; i < table.Rows.Count - 1; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    string str1 = table.Rows[i][j].NullableToString();
    
                    if (string.IsNullOrEmpty(str1) || str1 == "")
                        str1 = "	";
    
                    if (j == table.Columns.Count - 1)
                    {
                        strBuilder.AppendLine(str1);
                    }
                    else
                    {
                        str1 += "	";
                        strBuilder.Append(str1);
                    }
                }
            }
    
            streamWriter.WriteLine(strBuilder.ToString());
            streamWriter.Close();
            fileStream.Close();
            return true;
        }
        public static bool DataTable_To_TXT(DataTable table, string fullName, ref string Msg)
        {
            PathHelper.Check_Directory(fullName);
            if (table == null || table.Rows.Count == 0)
            {
                Msg += fullName + " 对应的 DataTable 没有数据";
                return false;
            }
            int[] iColumnLength = new int[table.Columns.Count];
            FileStream fileStream = new FileStream(fullName, FileMode.Create);
            StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.Unicode);
            StringBuilder strBuilder = new StringBuilder();
    
            for (int i = 0; i < table.Columns.Count; i++)
            {
                int iLength = 0;
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    if (iLength < (table.Rows[j][i].ToString()).Length)
                    {
                        iLength = (table.Rows[j][i].ToString()).Length;
                        if ((table.Rows[j][i].ToString()).Length < table.Columns[i].Caption.Length)
                        {
                            iLength = table.Columns[i].Caption.Length;
                        }
                    }
    
                }
                iColumnLength[i] = iLength + 1;
            }
    
            for (int j = 0; j < table.Columns.Count; j++)
            {
                string str1 = table.Columns[j].Caption;
                int iLength = str1.Length;
                int iColumnWidth = iColumnLength[j] + 4;
    
                for (int k = iLength; k < iColumnWidth; k++)
                {
                    str1 += " ";
                }
    
                if (j == table.Columns.Count - 1)
                {
                    strBuilder.AppendLine(str1);
                }
                else
                {
                    strBuilder.Append(str1);
                }
            }
    
            for (int i = 0; i < table.Rows.Count - 1; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    string str1 = table.Rows[i][j].NullableToString();
                    if (string.IsNullOrEmpty(str1))
                        str1 = "";
                    int iLength = str1.Length;
                    int iColumnWidth = iColumnLength[j] + 4;
    
                    for (int k = iLength; k < iColumnWidth; k++)
                    {
                        str1 += " ";
                    }
                    if (j == table.Columns.Count - 1)
                    {
                        strBuilder.AppendLine(str1);
                    }
                    else
                    {
                        strBuilder.Append(str1);
                    }
                }
            }
    
            streamWriter.WriteLine(strBuilder.ToString());
            streamWriter.Close();
            fileStream.Close();
            return true;
        }
        //****************************************************************************************
  • 相关阅读:
    标识符
    注释
    关键字
    第一个JAVA程序
    JAVA运行机制
    每个程序员都应该知道的延迟数
    构建WebDriverAgent时报错“xxx: no identity found Command CodeSign failed with a nonzero exit code”解决办法
    执行 sh ./Scripts/bootstrap.sh -d 时提示“xcrun: error: unable to find utility “simctl”, not a developer tool or in PATH ”错误解决办法
    stf ios版本部署
    centos 7安装一机多控stf客户端
  • 原文地址:https://www.cnblogs.com/ilookbo/p/4727560.html
Copyright © 2020-2023  润新知