5

我正在尝试学习如何使用 LINQTOExcel 来查询 CSV 文件。按照网站上的教程,我调整了他们的示例以处理我的数据(文件名通过 OpenDialog 组件传递给它):

var csv  = new ExcelQueryFactory(filename);
var test = from c in csv.Worksheet<TestData>()
           select c;

foreach(var t in test)
{
    Console.WriteLine(t.Contract_Id);
}

我有一个单独的 TestData 类/模型,如下所示:

class TestData
{
    public string Transaction_Id { get; set; }

    public string Value_Date { get; set; }

    public string Transmit_Date { get; set; }

    public string Transmit_Time { get; set; }

    public string Contract_Id { get; set; }

    public string Contract_Amount { get; set; }

    public string Contract_Rage { get; set; }

    public string TestAmount { get; set; }

    public string Employer_Code { get; set; }

    public string Test_Acceptor { get; set; }

    public string Institution_Id { get; set; }
}

但是当我遍历时,每个项目的所有值都是'null'。我在某处错过了一步吗?

CSV 数据示例:

transaction_id,value_date,transmit_date,transmit_time,contract_no,contract_amount,instalment,test_amount,employer_code,test_acceptor,institution_id
35454521,20111230,20120102,2:23:12,1442,1714.56,1,285.76,0,643650,a
4

1 回答 1

0

CSV 文件需要一个与属性名称匹配的标题行:

Transaction_Id,Value_Date,Transmit_Date,Transmit_Time,Contract_Id,Contract_Amount,Contract_RageTestAmount,Employer_Code,Test_Acceptor,Institution_Id
35454521,20111230,20120102,2:23:12,1442,1714.56,1,285.76,0,643650
于 2012-02-04T11:18:01.737 回答