首页 > 解决方案 > 如何将来自每个文本框多行的多个文本框的信息写入 CSV 文件?

问题描述

所以我正在编写一个程序,我在一个文本框中输入 3 个值,然后在另一个文本框中显示(输入文本框然后被清除),当你点击“确认”按钮时,你会得到一个提示你想要保存的位置这个文件和名字。

我现在有这个代码:

        private void btnBevestig_Click(object sender, EventArgs e) // Plek kiezen van opslag, keuze verifieren
        {
            if(MessageBox.Show("Bent u zeker?", "Confirmatie", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
            {
                txtUID.Text = string.Empty;
                txtDescriptionButton.Text = string.Empty;
                txtArduinoContact.Text = string.Empty;
                txtArtikelNaam.Text = string.Empty;
            } 
        }

        private void btnInvoeren_Click(object sender, EventArgs e) 
        {
            try
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(txtInvUID.Text, "[^0-9]"))
                {
                    MessageBox.Show("Je mag alleen nummers invoeren.");
                    txtInvUID.Text = txtInvUID.Text.Remove(txtInvUID.Text.Length - 1);
                }
                if (string.IsNullOrEmpty(txtInvUID.Text)) {
                    MessageBox.Show("Het vakje UID is leeg", "Informatie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else if(string.IsNullOrEmpty(txtInvDescriptionButton.Text)) {
                    MessageBox.Show("Het vakje Description button is leeg", "Informatie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else if (string.IsNullOrEmpty(txtInvArduinoContact.Text))
                {
                    MessageBox.Show("Het vakje Arduino contact is leeg", "Informatie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    txtUID.Text = txtInvUID.Text + "\r\n" + txtUID.Text;
                    txtDescriptionButton.Text = txtInvDescriptionButton.Text + "\r\n" + txtDescriptionButton.Text;
                    txtArduinoContact.Text = txtInvArduinoContact.Text + "\r\n" + txtArduinoContact.Text;

                    txtInvUID.Text = string.Empty;
                    txtInvDescriptionButton.Text = string.Empty;
                    txtInvArduinoContact.Text = string.Empty;

                }
            }   
            catch(Exception error)
            {
                MessageBox.Show(error.Message, "Fout", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

简而言之:txtUID、txtDescriptionButton 和 txtArduinoContact 是保存信息的文本框,我需要将此信息写入 CSV 文件,并且第一行需要是 UID、DSCRBUTTON 和 ARDUICONTACT。

标签: c#winforms

解决方案


你可以使用这个方法。

public void WriteToCSV()
{
   var csv = new StringBuilder();
   for (int i = 0; i < txtInvUID.Lines.Length; i++)
   {
       var newLine = string.Format("{0}", txtInvUID.Lines[i] + Environment.NewLine);
       csv.AppendLine(newLine);
   }

   for (int i = 0; i < txtUID.Lines.Length; i++)
   {
       var newLine = string.Format("{0}", txtUID.Lines[i] + Environment.NewLine);
       csv.AppendLine(newLine);
   }

        

   for (int i = 0; i < txtInvDescriptionButton.Lines.Length; i++)
   {
       var newLine = string.Format("{0}", txtInvDescriptionButton.Lines[i] + Environment.NewLine);
       csv.AppendLine(newLine);
   }

   for (int i = 0; i < txtDescriptionButton.Lines.Length; i++)
   {
       var newLine = string.Format("{0}", txtDescriptionButton.Lines[i] + Environment.NewLine);
       csv.AppendLine(newLine);
   }

   for (int i = 0; i < txtInvArduinoContact.Lines.Length; i++)
   {
       var newLine = string.Format("{0}", txtInvArduinoContact.Lines[i] + Environment.NewLine);
       csv.AppendLine(newLine);
   }

   for (int i = 0; i < txtArduinoContact.Lines.Length; i++)
   {
       var newLine = string.Format("{0}", txtArduinoContact.Lines[i] + Environment.NewLine);
       csv.AppendLine(newLine);
   }


   string fileName = @"D:\WriteText.csv";
   if (File.Exists(fileName))
        System.IO.File.AppendAllText(fileName, csv.ToString());
    else
        System.IO.File.WriteAllText(fileName, csv.ToString());
}

推荐阅读