WPF C# CSV人事管理系统

guykilcj  于 2023-03-09  发布在  C#
关注(0)|答案(1)|浏览(121)

我希望每当用户单击"添加人员"按钮时,personID都会增加,但每次再次运行时,增量又从1开始。此外,列名总是显示在用户的每次输入中,我只希望它在csv文件中显示一次。
我只是一个学生,刚开始接触C#。

Person.cs

namespace WPFApp
{
    public class Person
    {
        [CsvColumn(Name = "Person ID", FieldIndex = 1)]
        public string? personID { get; set; }

        [CsvColumn(Name = "Last Name", FieldIndex = 2)]
        public string? LastName { get; set; }
        
        [CsvColumn(Name = "First Name", FieldIndex = 3)]
        public string? FirstName { get; set; }

        [CsvColumn(Name = "Middle Name", FieldIndex = 4)]
        public string? MiddleName { get; set; }
    }
}
AddPerson.xaml.cs

namespace WPFApp
{
    /// <summary>
    /// Interaction logic for AddPerson.xaml
    /// </summary>
    public partial class AddPerson : Page
    {
        int personID = 0;

        public AddPerson()
        {
            InitializeComponent();

        }
        
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {

            personID++;

            var personList = new List<Person>()
            {
                new Person{ personID = personID.ToString(), LastName = LastNameBox.Text, FirstName = FirstNameBox.Text, MiddleName = MiddleNameBox.Text}
            };
            var csvFileDescription = new CsvFileDescription
            {
                FirstLineHasColumnNames = true,
                SeparatorChar = ','

            };

            var csvContext = new CsvContext();
            using (TextWriter writer = new StreamWriter("PersonFIles.csv", true))
            {
                csvContext.Write(personList, writer, csvFileDescription);
            }

            

            MessageBox.Show("Person has been added! " + "Person ID: " + personID);

            LastNameBox.Clear();
            FirstNameBox.Clear();
            MiddleNameBox.Clear();
        }
    }
}

PersonFIles.csv

Person ID,Last Name,First Name,Middle Name
Person ID,Last Name,First Name,Middle Name
1,Sample Last,Sample First,Sample Middle
Person ID,Last Name,First Name,Middle Name
2,James,Lebron,King

AddPerson.xaml

a64a0gku

a64a0gku1#

namespace WPFApp
{
    /// <summary>
    /// Interaction logic for AddPerson.xaml
    /// </summary>
    public partial class AddPerson : Page
    {
        int personID = 0;
        string csvFilePath = "PersonFiles.csv";

        public AddPerson()
        {
            InitializeComponent();
            // Read personID from CSV file
            if (File.Exists(csvFilePath))
            {
                using (var reader = new StreamReader(csvFilePath))
                {
                    // Skip the first line if it contains column names
                    if (reader.ReadLine()?.StartsWith("personID") == true)
                    {
                        reader.ReadLine();
                    }
                    // Read the last personID from the file and increment it
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        var values = line.Split(',');
                        if (values.Length > 0)
                        {
                            if (int.TryParse(values[0], out int id) && id > personID)
                            {
                                personID = id;
                            }
                        }
                    }
                }
            }
        }
        
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            // Increment personID
            personID++;

            // Create Person object and add to list
            var personList = new List<Person>()
            {
                new Person{ personID = personID.ToString(), LastName = LastNameBox.Text, FirstName = FirstNameBox.Text, MiddleName = MiddleNameBox.Text}
            };

            // Write list to CSV file
            var csvFileDescription = new CsvFileDescription
            {
                FirstLineHasColumnNames = false, // Don't write column names again
                SeparatorChar = ','
            };
            var csvContext = new CsvContext();
            using (TextWriter writer = new StreamWriter(csvFilePath, true))
            {
                csvContext.Write(personList, writer, csvFileDescription);
            }

            // Show message box and clear input fields
            MessageBox.Show("Person has been added! " + "Person ID: " + personID);
            LastNameBox.Clear();
            FirstNameBox.Clear();
            MiddleNameBox.Clear();
        }
    }
}

相关问题