csv读取数据表会使一行成为列名

本文关键字:一行 数据表 读取 csv | 更新日期: 2023-09-27 18:20:52

我正在从数据表中读取csv文件,然后发生了一些事情。

这是读取的

 OpenFileDialog openFileDialog1 = new OpenFileDialog();
 openFileDialog1.Filter = "CSV Files|*.csv";
 openFileDialog1.Title = "Select a CSV File";
 if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;", System.IO.Path.GetDirectoryName(openFileDialog1.FileName));
            string cmdString = string.Format("SELECT * FROM {0}", System.IO.Path.GetFileName(openFileDialog1.FileName));
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdString, connString);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);
            TableBuilder b = new TableBuilder(tbl);
            List<SingleTable> tablelist = b.TableList;
        }

当我在可视化工具中查看数据表时,它看起来像![第一行不是行,而是列标题][1]

原始csv看起来像这个

[原始csv][2]

有人能告诉我为什么数据表将第一行转换为列吗?我该怎么做才能保住第一排呢?

csv读取数据表会使一行成为列名

不确定这是否是您所需要的,因为您的问题中似乎缺少了一些内容。但您可以将连接字符串更改为:

string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;HDR=No", System.IO.Path.GetDirectoryName(openFileDialog1.FileName));

这将告诉连接第一行不是标题行。

尝试告诉OLEDB提供程序文件没有头

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;HDR=no;

或者看看这个SO答案:https://stackoverflow.com/a/4598862/284240

通常,如果数据中嵌入了额外的",",这将把每个逗号都视为一个单独的列。。我不得不检查导致字段偏移的列数据。。还有您正在创建的Header。。?