在 C# 中创建 CSV 文件

发布于 2024-11-01 16:06:51 字数 1921 浏览 0 评论 0原文

我正在尝试创建一个 csv 文件,其中每个条目均由用户输入。输入一组值后,应继续输入下一组值。然而我似乎无法做到这一点。每次我运行该程序时,它都会替换以前的值。

using System;
using System.IO;
using System.Collections.Generic;


namespace FileAccess
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string FName;
            string LName;
            string Dpt;
            string Grade;
            string NumberOfHours;

            string file_name = "EmployeeDetails.txt";

            System.IO.StreamWriter objWriter;
            objWriter = new System.IO.StreamWriter(file_name);

            String[] ArrayDetails = new string[5];

            Console.Write("Enter Emp First Name: ");
            FName=Console.ReadLine();
            Console.Write("Enter Emp Last Name: ");
            LName = Console.ReadLine();
            Console.Write("Enter Emp Department: ");
            Dpt = Console.ReadLine();
            Console.Write("Enter Employee Grade: ");
            Grade = Console.ReadLine();
            Console.Write("Enter Number Of Hours Worked: ");
            NumberOfHours = Console.ReadLine();

            ArrayDetails[0] = FName;
            ArrayDetails[1] = LName;
            ArrayDetails[2] = Dpt;
            ArrayDetails[3] = Grade;
            ArrayDetails[4] = NumberOfHours;

            // Create new list of strings
            List<string> LDetails = new List<string>(); // Create new list of strings
            LDetails.Add(FName); // Add string 1
            LDetails.Add(LName); // 2
            LDetails.Add(Dpt); // 3
            LDetails.Add(Grade); // 4
            LDetails.Add(NumberOfHours); // 5
            //OutPut the data into the file
            string LDetailsCSV = string.Join(",", LDetails.ToArray());
            Console.WriteLine(LDetailsCSV);
            objWriter.Write(LDetailsCSV);
            objWriter.Close();
        }
    }
}

请一些建议。

I am trying to created a csv file where each entry is inputed by the user. Once one set of of values have been entered it should move on to the next. However I cannot seem to do the bit. Each time i run the program, it replaces the previous values.

using System;
using System.IO;
using System.Collections.Generic;


namespace FileAccess
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string FName;
            string LName;
            string Dpt;
            string Grade;
            string NumberOfHours;

            string file_name = "EmployeeDetails.txt";

            System.IO.StreamWriter objWriter;
            objWriter = new System.IO.StreamWriter(file_name);

            String[] ArrayDetails = new string[5];

            Console.Write("Enter Emp First Name: ");
            FName=Console.ReadLine();
            Console.Write("Enter Emp Last Name: ");
            LName = Console.ReadLine();
            Console.Write("Enter Emp Department: ");
            Dpt = Console.ReadLine();
            Console.Write("Enter Employee Grade: ");
            Grade = Console.ReadLine();
            Console.Write("Enter Number Of Hours Worked: ");
            NumberOfHours = Console.ReadLine();

            ArrayDetails[0] = FName;
            ArrayDetails[1] = LName;
            ArrayDetails[2] = Dpt;
            ArrayDetails[3] = Grade;
            ArrayDetails[4] = NumberOfHours;

            // Create new list of strings
            List<string> LDetails = new List<string>(); // Create new list of strings
            LDetails.Add(FName); // Add string 1
            LDetails.Add(LName); // 2
            LDetails.Add(Dpt); // 3
            LDetails.Add(Grade); // 4
            LDetails.Add(NumberOfHours); // 5
            //OutPut the data into the file
            string LDetailsCSV = string.Join(",", LDetails.ToArray());
            Console.WriteLine(LDetailsCSV);
            objWriter.Write(LDetailsCSV);
            objWriter.Close();
        }
    }
}

Some advise please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疧_╮線 2024-11-08 16:06:51

StreamWriter 构造函数 将覆盖您正在使用的文件(如果存在)。有一个重载构造函数,您可以将布尔值作为第二个参数传递给它来告诉它附加:

System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(file_name, true);

此外,您使用的 Write 方法不会附加换行符。如果您使用 WriteLine 方法,它会。

 Console.WriteLine(LDetailsCSV);
 objWriter.WriteLine(LDetailsCSV);
 objWriter.Close();

The StreamWriter constructor will overwrite the file you are using if it exists. There is an overloaded constructor to which you can pass a boolean as a second argument to tell it to append:

System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(file_name, true);

In addition, the Write method you are using will not append a line break. If you use the WriteLine method, it will.

 Console.WriteLine(LDetailsCSV);
 objWriter.WriteLine(LDetailsCSV);
 objWriter.Close();
影子是时光的心 2024-11-08 16:06:51

您必须以附加模式打开文件,如下所示:

System.IO.StreamWriter objWriter;
objWriter = new File.AppendText(file_name);

You have to open the file in append mode, like this:

System.IO.StreamWriter objWriter;
objWriter = new File.AppendText(file_name);
无可置疑 2024-11-08 16:06:51

就你而言,我认为你最好这样做:

File.AppendAllText("EmployeeDetails.txt", string.Join(",",new [] {FName,LName,Dpt,Grade,NumberOfHours}));

In your case I think you are better off doing something like:

File.AppendAllText("EmployeeDetails.txt", string.Join(",",new [] {FName,LName,Dpt,Grade,NumberOfHours}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文