【问题标题】:reading all the columns from CSV file从 CSV 文件中读取所有列
【发布时间】:2019-04-05 10:49:19
【问题描述】:

我对 C# 很陌生。我需要从 CSV 文件中读取所有列。 在我的 CSV 中,我有:日期,test1(在 test1 下有一些随机数据),lowerlimit(test1 数据中的最小数字,在所有下限行中复制相同。最后一列是 highlimit(test1 数据中的最高数字)。

这是为了计算过程能力。

我发现下面的代码只读取第一列。 但我需要阅读所有的专栏

 public static DataTable OpenCSV(string filePath)
    {
        DataTable dt = new DataTable(); 
        FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

        StreamReader sr = new StreamReader(fs);
        string strLine = "";
        string[] aryLine = null;
        string[] tableHead = null;
        int columnCount = 0;       
        bool IsFirst = true;
        while ((strLine = sr.ReadLine()) != null)
        {
            if (IsFirst == true)
            {
                tableHead = strLine.Split(',');
                IsFirst = false;
                columnCount = tableHead.Length;
                for (int i = 0; i < columnCount; i++)
                {
                    DataColumn dc = new DataColumn(tableHead[i]);
                    dt.Columns.Add(dc);                     
                }
            }
            else
            {
                aryLine = strLine.Split(',');
                DataRow dr = dt.NewRow();
                for (int j = 0; j < columnCount; j++)
                {
                    dr[j] = aryLine[j];
                }
                dt.Rows.Add(dr);
            }
        }
        if (aryLine != null && aryLine.Length > 0)
        {
            dt.DefaultView.Sort = tableHead[0] + " " + "asc";
        }
        sr.Close();
        fs.Close();
        return dt;
    }

    DataTable dt;

    public int Val1 { get; private set; }
    public int Val2 { get; private set; }
    public int Cp { get; private set; }

    private void btOK_Click(object sender, EventArgs e)
    {
        dt = OpenCSV(textBox1.Text);
        foreach (DataRow dr in dt.Rows)
        {
            // Get the first column
            checkedListBox1.Items.Add(dr["Date"]);  
        }

我希望在读取文件中的所有列后进行一些计算

【问题讨论】:

  • 永远不要编写自己的 CSV 阅读器。 github.com/phatcher/CsvReader
  • 此代码逐行读取。将每一行拆分为列,并将列拆分为行。为什么你认为它只读取第一列?
  • 你能告诉我我应该做哪些修改来阅读上面代码中的所有列(手动给出标题)
  • 拆分后**
  • 正如@vik_78 所说,它已经读取了所有列!

标签: c# csv


【解决方案1】:

在 foreach(dt.Rows 中的 DataRow dr)中,您的 dr 拥有所有列。您可以通过在索引器中输入不同的列名来访问这些值。例如dr["test1"], dr["lowerlimit"], dr["higherlimit"] 等

【讨论】:

  • 你好,我需要把测试的所有数据放在一个数组中
【解决方案2】:

这是另一种读取 csv 文件的方法:

List<string> lstDates = new List<string>();
List<string> lstTest = new List<string>();
string[] rows = File.ReadAllLines(filePath);
foreach(string r in rows)
{
  string[] cols = r.split(',');
  lstDates.Add(cols[0]);
  lstTest.add(cols[1]);
  //you can add other columns to lists
}
//if you need the lists to be arrays you can convert them
string[] arrDates = lstDates.ToArray();

【讨论】:

  • 嗨,我进行了拆分并从 CSV 文件中获取值。现在我需要在 csv 的那些列上添加一些数学运算。 (我想计算过程能力Cp,Cpk)
  • “添加一些数学运算”我认为是一个不同的问题,你应该用谷歌搜索它。此外,尚不清楚您到底想要什么。乍一看,我可以建议您;将值获取到数组后,您可以遍历它并将每个元素转换为整数而不是进行计算
猜你喜欢
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
相关资源
最近更新 更多