简体   繁体   English

用C#写入CSV文件

[英]Writing to CSV File in C#

I am creating a clock for clocking into a business. 我正在创建一个时钟来进行业务。 Here is my code: 这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    String Code;
    String Name;
    String InOut;
    Boolean Luke = true;
    String csvPath = "C:/users/luke/documents/C#/csvProject.csv";
    StringBuilder Header = new StringBuilder();
    StringBuilder csvData = new StringBuilder();



    public Form1()
    {
        InitializeComponent();
        FormBorderStyle = FormBorderStyle.None;
        WindowState = FormWindowState.Maximized;
        TopMost = true;

        Header.AppendLine("Timestamp, Name");
        File.AppendAllText(csvPath, Header.ToString());
        textBox1.Font = new Font("Arial", 30, FontStyle.Bold);


    }

    private void button_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        Code = Code + button.Text;
        textBox1.Text = Code;
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            FormBorderStyle = FormBorderStyle.Sizable;
            WindowState = FormWindowState.Normal;
            TopMost = false;
        }
    }

    private void button13_Click(object sender, EventArgs e)
    {
        //clear
        Code = null;
        textBox1.Text = Code;
    }

    private void button10_Click(object sender, EventArgs e)
    {
        //in or out
        DateTime timeStamp = DateTime.Now;
        if (Code == "123")
        {
            Name = "Luke";
        }
        Button button = (Button)sender;
        csvData.AppendLine(timeStamp + "," + Name + "," + button.Text);
        File.AppendAllText(csvPath, csvData.ToString());
        Code = null;
        textBox1.Text = Code;
    }

    private void button14_Click(object sender, EventArgs e)
    {

    }
}
}

My layout consists of a number pad, in button, and out button. 我的布局由数字键盘,“输入”按钮和“输出”按钮组成。 When the user presses the in button after they enter their code, the program should write in the CSV file: Timestamp, Name, In. 当用户输入代码后按下in按钮时,程序应在CSV文件中写入:时间戳记,名称,输入。 When I tested the code by clocking in, the program writes one row correctly. 当我通过时钟测试代码时,该程序正确写入了一行。 When I clock in and then clock out, it creates two rows of me clocking in and one row of me clocking out. 当我先上班然后再下班时,它会创建两行我上班和一行我下班。 I was wondering if anyone could help me find what is going wrong in the code. 我想知道是否有人可以帮助我找到代码中出了什么问题。 Thanks. 谢谢。

将其写入文件后,需要清空csvData。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM