1

下面是用于解析csv文件并使用客户端和客户端名称填充 2X2 表的按钮的代码。我在网上查看过,但找不到合适的示例来说明我想做的事情,使用 VS 2012 和 C# 我想通读文件,只取出并插入客户端和客户端名称,但目前我的代码只需要一切文件内。

如果找到关键字,我的预期方法是csv阅读并阅读它;使用它然后继续搜索,只是想知道是否有人可以告诉我如何?

例如,我想在laClient列表中进行搜索,如果我发现HTY我想在此之后获取接下来的 6 个字符并继续我的搜索。

private void btnUpload_Click(object sender, EventArgs e)
{
    //Browse for file
    OpenFileDialog ofd = new OpenFileDialog();
    //Only show .csv files
    ofd.Filter = "Microsoft Office Excel Comma Separated Values File|*.csv";
    DialogResult result = ofd.ShowDialog();
        
    //If the user selects a valid file 
    if (result == DialogResult.OK)
    {
        //File is delimited by a comma
        char[] laClientDelim = { ',' };

        //New object for string manipulation
        objStringManipulation = new StringManipulation();
            
        // Parse the csv file
        List<string[]> lsClientList = objStringManipulation.parseCSV(ofd.FileName,  laClientDelim);

        foreach (string[] laClient in lsClientList)
        {
            //Create new object for manipulating the database
            objSqlCommands = new SqlCommands("Client", "ClientName");
            laClient[8].
                

            //Inserts the client info into datbase
            objSqlCommands.sqlCommandInsertorUpdate("INSERT", laClient[0]);
            //Refreshs the Client table on display from the 
            this.clientTableAdapter.Fill(this.ImpDataSet.Client);

            //update the view 
            dgvClientlst.Update(); 
            
        }
    }
}

csv 数据看起来像这样(下图),由于这个布局的质量很差,我试图附上一张图片以提高可读性,但我没有 10 名声望,所以我无法这样做。

ISBU,ShareName,Status,Share CCY,Benchmark,NAV Freq,CLASSCODE,SIMULATION,HEDGED,FUNDCCY

1,1,2,Cj0162289439,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CVT),AC,n/a,N,,

2,1,2,Cj0622164928,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CCT),ACEUR,n/a,N,,

3,1,2,Cj0212851702,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CST),ACHEUR,n/a,Y,,

4,1,2,Cj0042850808,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CET),AD,n/a,N,,

5,1,2,Cj0212851884,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CDT),ADHEUR,n/a,Y,,

6,1,2,Cj0162849209,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CHT),EC,n/a,N,,

7,1,2,Cj0622165065,FGHT Global Funds - Asia incl Korea Equity,未激活,美元,每日下午 5:00 (CET),ECEUR,n/a,N,,

8,1,2,Cj0112316214,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CGT),ED,n/a,N,,

9,4,2,Cj0162193169,FGHT Global Funds - Asia incl Korea Equity,Activated,USD,Daily at 5:00pm (CBT),IC,n/a,N,,

4

1 回答 1

0

您应该能够读取 csv,并使用以下代码获取单个值:

string[] records = File.ReadAllLines(fileName); // read the file completely line by line

// loop through all lines
foreach (string record in records)
{
    string[] fields = record.Split(splitChar);  // reads all the single values per line. 'splitChar' should be the delimiter.
}
于 2013-08-02T09:59:21.043 回答