读取csv并将数据放置在列表框中。

问题描述 投票:0回答:1
using System.IO;

namespace HW10
{
struct Employee
{
    public string LastName;
    public string FirstName;
    public string IDNumber;
    public string Department;
    public string Position;
    public string PayType;
    public double HoursWorked;
    public double PayRate;
}
public partial class frmPayroll : Form
{
    //Create a list to hold the data from the input file
    private List<Employee> EmployeeList = new List<Employee>();

    public frmPayroll()
    {
        InitializeComponent();
    }

    private void frmPayroll_Load(object sender, EventArgs e)
    {

    }

    private void btnOpen_Click(object sender, EventArgs e)
    {
        //Declare local variables
        DialogResult ButtonClicked;
        string InputRecord;
        string[] InputFields;

        Employee worker;

        //Show the OpenFileDialog box for user to select file
        openFileDialog1.InitialDirectory = Environment.CurrentDirectory;
        openFileDialog1.FileName = "Payroll.csv";

        ButtonClicked = openFileDialog1.ShowDialog();

        if(ButtonClicked == DialogResult.Cancel)
        {
            MessageBox.Show("You did NOT select a file.");
            return;
        }
        //Declare the streamreader variable to access the file
        StreamReader inputFile = File.OpenText(openFileDialog1.FileName);

        //Set up a loop to read every record, skipping the first record
        InputRecord = inputFile.ReadLine();

        while(!inputFile.EndOfStream)
        {
            InputRecord = inputFile.ReadLine();
            InputFields = InputRecord.Split(',');

            worker.LastName = InputFields[0];
            worker.FirstName = InputFields[1];
            worker.IDNumber = InputFields[2];
            worker.Department = InputFields[3];
            worker.Position = InputFields[4];
            worker.PayType = InputFields[5];                
            worker.HoursWorked = double.Parse(InputFields[6]);
            worker.PayRate = double.Parse(InputFields[7]);

            //Add this worker struc to the list declared above
            EmployeeList.Add(worker);

            //Display the name in the Listbox on the form
            listPayroll.Items.Add(worker.LastName + "," + worker.FirstName);
        }

        //Close the input file
        inputFile.Close();
        btnOpen.Enabled = false;
    }

    private void btnCompute_Click(object sender, EventArgs e)
    {
        listPayroll.Items.Clear();
        string DisplayHeader = String.Format("{0,-18}{1,14}{2,11}{3,13}",
            "Employee", "HoursWorked", "PayRate", "Gross Pay");
        listPayroll.Items.Add(DisplayHeader);
        DisplayHeader = String.Format("{0,-18}{1,14}{2,11}{3,13}",
        "Employee", "HoursWorked", "PayRate", "Gross Pay");
        listPayroll.Items.Add(DisplayHeader);

        foreach (Employee emp in EmployeeList)
        {
            double HoursWorked = 0.0;
            double PayRate = 0.0;
            double GrossPay = 0.0; 

            // Check for Salary or Hourly
            if(emp.PayType == "Hourly")
            {
                if(emp.HoursWorked > 40.0)
                {
                    PayRate = emp.PayRate * (emp.HoursWorked - 40.0);
                    HoursWorked = emp.PayRate * 40.0;
                }
                else 
                {
                    PayRate = 0.0;
                    HoursWorked = emp.PayRate * emp.HoursWorked;
                }
            }
            else //Salary paytype. No ot. Just striaght pay for 40 hrs
            {
                PayRate = 0.0;
                HoursWorked = emp.PayRate * 40.0;
            }

            GrossPay = HoursWorked + PayRate;

            //Display this data in the listbox
            string DisplayPayroll = String.Format("{0,-18}{1,14:c}{2,11:C{3,13:c}",
                emp.FirstName + " " + emp.LastName,
                HoursWorked, PayRate, GrossPay);
            listPayroll.Items.Add(DisplayPayroll);
        }
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        // This method closes the application
        this.Close();

当我试图从一个.csv文件中计算数据到一个列表框时,我遇到了问题。它在 "在列表框中显示此数据 "的代码块中抛出了一个Exception unhandled错误。我已经把系统IO和我需要在列表框中显示的所有字符串double。我确信这是一个非常简单的修复方法,但我是编码新手。任何帮助都将是非常感激的。

c# listbox
1个回答
0
投票

使用string.Split(',')来处理CSV数据是不够的。 CSV数据可以包含有逗号的字段。

https:/en.wikipedia.orgwikiComma-separated_values(逗号分隔的值)

我建议使用CSV库来帮助解析文件。谷歌搜索 "c# csv库 "应该会给你很多选择。

© www.soinside.com 2019 - 2024. All rights reserved.