zoukankan      html  css  js  c++  java
  • C# CSV 文件转换成DataTable

    {
                DataTable dt = new DataTable();
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                //记录每次读取的一行记录
                string strLine = "";
                //记录每行记录中的各字段内容
                string[] aryLine;
                //标示列数
                int columnCount = 0;
                //标示是否是读取的第一行
                bool IsFirst = true;
                bool IsTotalResult = false;
                //逐行读取CSV中的数据
                while ((strLine = sr.ReadLine()) != null)
                {
                    if (strLine == "" || strLine.Replace(",","").IsEmpty())
                    {
                        continue;
                    }
                    aryLine = strLine.Replace(" ","").Split(',');
                    if (strLine.Contains("Header_Data"))
                    {
                        IsTotalResult = true;
                        continue;
                    }
                    if (IsTotalResult)
                    {
                        if (strLine.Contains("Component_Data"))
                        {
                            string totalReuslt = dt.Rows[0]["Result"].ToString();
                            strSN = dt.Rows[0]["BarCode"].ToString();
                            if (totalReuslt.Contains('F'))
                            {
                                IsTotalResult = false;
                                dt = new DataTable();
                                IsFirst = true;
                                continue;
                            }
                            else
                            {
                                sr.Close();
                                fs.Close();
                                return true;
                            }
                        }
                        if (IsFirst == true)
                        {
                            IsFirst = false;
                            columnCount = aryLine.Length;
                            //创建列
                            for (int i = 0; i < columnCount; i++)
                            {
                                DataColumn dc = new DataColumn(aryLine[i].Trim());
                                dt.Columns.Add(dc);
                            }
                        }
                        else
                        {
                            DataRow dr = dt.NewRow();
                            for (int j = 0; j < columnCount; j++)
                            {
                                dr[j] = aryLine[j].Trim();
                            }
                            dt.Rows.Add(dr);
                        }
                        continue;
                    }
                    if (IsFirst == true)
                    {
                        IsFirst = false;
                        columnCount = aryLine.Length;
                        //创建列
                        for (int i = 0; i < columnCount; i++)
                        {
                            DataColumn dc = new DataColumn(aryLine[i].Trim());
                            dt.Columns.Add(dc);
                        }
                    }
                    else
                    {
                        DataRow dr = dt.NewRow();
                        for (int j = 0; j < columnCount; j++)
                        {
                            dr[j] = aryLine[j].Trim();
                        }
                        dt.Rows.Add(dr);
                    }
                }
                sr.Close();
                fs.Close();
    }
    
  • 相关阅读:
    hudson中 ANT 编译警告: 编码 UTF-8 的不可映射字符解决方法
    Jmeter与hudson,ant集成
    Hudson配置路径
    python 面向对象:封装---对象的属性可以是另一个类创建的对象
    python 面向对象:封装
    python3 f-string格式化字符串的高级用法
    iOS微信支付无法直接返回APP的问题
    学习git&github
    Appium之xpath定位详解
    selenium等待方式详解
  • 原文地址:https://www.cnblogs.com/ILoveMyJob/p/10137292.html
Copyright © 2011-2022 走看看