简体   繁体   English

将数据写入CSV文件

[英]Write data to CSV file

I'm having a question about writing data to a CSV file. 我对将数据写入CSV文件有疑问。 I have a file named test.csv in which are 2 fields > accountnumber and relation ID. 我有一个名为test.csv的文件,其中有2个字段>帐号和关系ID。 Now I want to add another field next to it: IBAN. 现在,我想在其旁边添加另一个字段:IBAN。 The IBAN is the data from the first row which is validated by the SOAP function BBANtoIBAN . IBAN是来自第一行的数据,已通过SOAP函数BBANtoIBAN进行了验证

How can I keep the 2 rows of data accountnumbers and relation IDs in the CSV and add the IBAN in the 3rd row? 如何将两行数据帐号和关系ID保留在CSV中,并将IBAN添加到第三行?

This is my code so far: 到目前为止,这是我的代码:

using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
{
    List<List<string>> dataList = new List<List<string>>();
    TextFieldParser parser = new TextFieldParser(@"C:\CSV\test.csv");
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(";");
    while (!parser.EndOfData)
    {
        List<string> data = new List<string>();
        string row = parser.ReadLine();

        try
        {
            string resultIBAN = client.BBANtoIBAN(row);
            if (resultIBAN != string.Empty)
               data.Add(resultIBAN);
            else
               data.Add("Accountnumber is not correct.");
        }
        catch (Exception msg)
        {
            Console.WriteLine(msg);
        }
        dataList.Add(data);
    }  
}

I see it as: 我将其视为:

StreamReader sr = new StreamReader(@"C:\CSV\test.csv")
StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv")
while (sr.Peek() >= 0) 
{
    string line = sr.ReadLine(); 


try
{
       string[] rowsArray = line.Split(';');
       string row = rowsArray[0];
       string resultIBAN = client.BBANtoIBAN(row);
       if (resultIBAN != string.Empty)
       {
           line +=";"+ resultIBAN;
       }
       else
       {
            line +=";"+"Accountnumber is not correct.";
       }

 }
 catch (Exception msg)
 {
     Console.WriteLine(msg);
 }
 sw.WriteLine(line) 
 }
sr.Close();
sw.Close();

I would do something like this to parse the csv file, and add an extra item to the data list: 我会做这样的事情来解析csv文件,并在数据列表中添加一个额外的项目:

        List<List<string>> dataList = new List<List<string>>();
        string filename = @"C:\CSV\test.csv";
        using (StreamReader sr = new StreamReader(filename))
        {
            string fileContent = sr.ReadToEnd();

            foreach (string line in fileContent.Split(new string[] {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries))
            {
                List<string> data = new List<string>();
                foreach (string field in line.Split(';'))
                {
                    data.Add(field);
                }
                try
                {
                    string resultIBAN = client.BBANtoIBAN(data[0]);
                    if (resultIBAN != string.Empty)
                    {
                        data.Add(resultIBAN);
                    }
                    else
                    {
                        data.Add("Accountnumber is not correct.");
                    }
                }
                catch (Exception msg)
                {
                    Console.WriteLine(msg);
                }
                dataList.Add(data);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM