4

I am trying to read a CSV into a datatable.

The CSV maybe have hundreds of columns and only up to 20 rows.

It will look something like this:

+----------+-----------------+-------------+---------+---+
|  email1  |     email2      |   email3    | email4  | … |
+----------+-----------------+-------------+---------+---+
| ccemail1 | anotherccemail1 | 3rdccemail1 | ccemail |   |
| ccemail2 | anotherccemail2 | 3rdccemail2 |         |   |
| ccemail3 | anotherccemail3 |             |         |   |
| ccemail4 | anotherccemail4 |             |         |   |
| ccemail5 |                 |             |         |   |
| ccemail6 |                 |             |         |   |
| ccemail7 |                 |             |         |   |
| …        |                 |             |         |   |
+----------+-----------------+-------------+---------+---+

i am trying to use genericparser for this; however, i believe that it requires you to know the column names.

string strID, strName, strStatus;
using (GenericParser parser = new GenericParser())
{
    parser.SetDataSource("MyData.txt");

    parser.ColumnDelimiter = "\t".ToCharArray();
    parser.FirstRowHasHeader = true;
    parser.SkipStartingDataRows = 10;
    parser.MaxBufferSize = 4096;
    parser.MaxRows = 500;
    parser.TextQualifier = '\"';

    while (parser.Read())
    {
      strID = parser["ID"];  //as you can see this requires you to know the column names
      strName = parser["Name"];
      strStatus = parser["Status"];

      // Your code here ...
    }
}

is there a way to read this file into a datatable without know the column names?

4

4 回答 4

7

就是这么简单!

        var adapter = new GenericParsing.GenericParserAdapter(filepath);
        DataTable dt = adapter.GetDataTable();

这将自动为您完成所有工作。

于 2012-07-19T22:06:58.623 回答
2

我查看了源代码,您也可以通过列索引访问数据,就像这样

var firstColumn = parser[0]

将 0 替换为列号。列数可以使用

parser.ColumnCount
于 2012-07-19T21:48:35.783 回答
2

我对此不熟悉GenericParser,我建议使用类似TextFieldParser,FileHelpers或 this的工具CSV-Reader

但是这种简单的手动方法也应该有效:

IEnumerable<String> lines = File.ReadAllLines(filePath);
String header = lines.First();
var headers = header.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
DataTable tbl = new DataTable();
for (int i = 0; i < headers.Length; i++)
{
    tbl.Columns.Add(headers[i]);
}
var data = lines.Skip(1);
foreach(var line in data)
{
    var fields = line.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
    DataRow newRow = tbl.Rows.Add();
    newRow.ItemArray = fields;
}
于 2012-07-19T22:01:45.817 回答
1

我使用通用解析器来做到这一点。在第一次运行循环时,我得到列名,然后引用它们以将它们添加到列表中

就我而言,我已经对数据进行了透视,但如果它对某人有帮助,这里是一个代码示例

        bool firstRow = true;
        List<string> columnNames = new List<string>();
        List<Tuple<string, string, string>> results = new List<Tuple<string, string, string>>();

        while (parser.Read())
        {
            if (firstRow)
            {
                for (int i = 0; i < parser.ColumnCount; i++)
                {
                    if (parser.GetColumnName(i).Contains("FY"))
                    {
                        columnNames.Add(parser.GetColumnName(i));
                        Console.Log("Column found: {0}", parser.GetColumnName(i));
                    }
                }
                firstRow = false;
            }

            foreach (var col in columnNames)
            {
                double actualCost = 0;
                bool hasValueParsed = Double.TryParse(parser[col], out actualCost);
                csvData.Add(new ProjectCost
                {
                    ProjectItem = parser["ProjectItem"],
                    ActualCosts = actualCost,
                    ColumnName = col
                });
            }
        }
于 2018-11-13T19:07:04.083 回答