如何在C#中将数据附加到csv文件 [英] How do I append data to a csv file in C#

查看:108
本文介绍了如何在C#中将数据附加到csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码将文本插入到csv文件中:



i got this code to insert text into a csv file:

try
               {

                   for (int i = 0; i < this.TestCases; i++)
                   {


                       textClientNumber[i] = readClientNumber.ReadLine();
                       textCardNumber[i] = readCardNumber.ReadLine();

                       export.AddRow();
                       export["TransactionDate"]= DateTime.Now.ToString("yyyyMMdd");
                       export["Card Type"] = "MC";
                       export["Original Payment Reference"] = " ";
                       export["Retreival Reference"] = " ";
                       export["Credit/Debit Card Indicator"] = " ";
                       export["Institution_number"] = this.Institution_number;
                       export["Refund"] = cmbRefund.SelectedItem.ToString();
                       export["Card Number"] = textCardNumber[i].ToString();
                       export["Capture Method"] = cmbCaptureMethod.SelectedValue.ToString();
                       export["ECI Indicator / Rate Program"] = cmbEciIndicator.SelectedItem.ToString();
                       export["Client Number"] = textClientNumber[i].ToString();
                       export["Processor ID"] = " ";
                       export["Currency"] = cmbCurrency.SelectedValue.ToString();
                       int randnum1 = rand.Next(1, 999999);
                       string randnumstring = opClass.AmountToExport(randnum1);
                       export["Amount"] =randnumstring ;
                   }


                   export.ExportToFile("C:\\temp\\MCB2" + randnum + ".csv");
                   this.Excelfilename = "MCB2" + randnum;
                   MessageBox.Show("Csv File created, You can find the file in C:\\temp\\" + this.Excelfilename);

               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.ToString());
               }







可以告诉我如何开始添加行从上次使用的行或者另一种技术怎么做?



我尝试过:



我尝试使用此代码将行追加到现有的.csv文件中:






Can someoent ell me how can i begin add rows from the last used row or maybe another technique how to do it ?

What I have tried:

I tried this code to append rows to the exisiting .csv file:

  try
                {
                    StreamReader reader = File.OpenText(this.Excelfilename + ".csv");
                    
                }
                catch (Exception Ex)
                {
                    Console.WriteLine(Ex.ToString());


                var firstLetterCharacters = this.Excelfilename.TakeWhile(Char.IsLetter);
                string filename = new string(firstLetterCharacters.ToArray());
                }




if (filename == "MCB")
            {
                try
                {
                        

                        for (int i = 0; i < this.TestCases; i++)
                        {

                            textClientNumber[i] = readClientNumber.ReadLine();
                            textCardNumber[i] = readCardNumber.ReadLine();

                            export.AddRow();

                            export["TransactionDate"] = DateTime.Now.ToString("yyyyMMdd");
                            export["Card Type"] = "MC";
                            export["Original Payment Reference"] = " ";
                            export["Retreival Reference"] = " ";
                            export["Credit/Debit Card Indicator"] = " ";
                            export["Institution_number"] = this.Institution_number;
                            export["Refund"] = cmbRefund.SelectedItem.ToString();
                            export["Card Number"] = textCardNumber[i].ToString();
                            export["Capture Method"] = cmbCaptureMethod.SelectedValue.ToString();
                            export["ECI Indicator / Rate Program"] = cmbEciIndicator.SelectedItem.ToString();
                            export["Client Number"] = textClientNumber[i].ToString();
                            export["Processor ID"] = " ";
                            export["Currency"] = cmbCurrency.SelectedValue.ToString();
                            int randnum1 = rand.Next(1, 999999);
                            string randnumstring = opClass.AmountToExport(randnum1);
                            export["Amount"] = randnumstring;

                            

                        }
                    if (File.Exists("C:\\temp\\" + this.Excelfilename + ".csv"))
                    {
                        File.Delete(this.Excelfilename);
                        export.ExportToFile("C:\\temp\\" + this.Excelfilename + ".csv");
                    }







旧数据完好无损,将被丢失并被替换为最新数据。




everything runs perfectly by the old data will be lost and be replaced to the newest data.

推荐答案

您有两种方法:可以使用 File.AppendText Method(String)(System.IO) [ ^ ]或使用流使用附加选项:

You have two methods: you can append the text, using File.AppendText Method (String) (System.IO)[^] or by using a stream with the Append option:
using (FileStream fs = new FileStream(fileName,FileMode.Append, FileAccess.Write))
    {
    using (StreamWriter sw = new StreamWriter(fs))
        {
        sw.WriteLine(myNewCSVLine);
        }
    }



唯一的另一种方法是读取整个文件(使用字符串集合或CSVReader)附加数据,以及把它全部写回一个新文件。


The only other way is to read the entire file (using a string collection, or a CSVReader) append your data, and write it all back to a new file.


这篇关于如何在C#中将数据附加到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆