使用C#将标头添加到CSV文件程序 [英] Adding a header to a CSV file program using C#

查看:64
本文介绍了使用C#将标头添加到CSV文件程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 InitializeComponent();

 conDB = new csvfile();
 conDB.Path = "E:\\BR_AttendanceDownloadLogs" + 
 DateTime.Today.ToString("yyyyMMdd") + ".csv";

 fillCombo();

作为编程的初学者,也是第一次在c#中对csv文件进行CRUD.我指的是此链接:

As beginner of programming and a very first time doing CRUD to a csv file in c#. I refer to this link:https://social.msdn.microsoft.com/Forums/en-US/27a98772-2dc6-4197-9f75-6cc119d4a88a/how-to-edit-a-line-from-a-text-file-using-c?forum=Vsexpressvcs that I've been located my source and useful to my program. But it was not satisfy to my specific satisfaction. I want to add header from it. Yes, I successfully added a header using this code.

   String newFileName = "E:\\BR_AttendanceDownloadLogs" +
   DateTime.Today.ToString("yyyyMMdd") + ".csv";

        string clientHeader = "\"EmployeeCode\"" + ",\"" + "Date" + "\",\""+
        "Time" + "\",\"" + "Type" + "\",\"" + "Remarks" + "\"" +
        Environment.NewLine;

        File.WriteAllText(newFileName, clientHeader);

        conDB.InsertEntrie(txtidno.Text, DatePicker.Text, TimePicker.Text, 
        cmbtype.Text, txtremarks.Text);
            //txtID.Text = Convert.ToString(conDB.Entries()-1);
            txtvalue = Convert.ToString(conDB.Entries() - 1);

        fillCombo();
        txtidno.Text = "";
        DatePicker.Text = "";
        TimePicker.Text = "";
        cmbtype.Text = "";
        txtremarks.Text = "";

但是在成功添加我的标头后,如下所示:雇员代码",日期",时间",类型",备注"代码流被标头中的这种类型的字段所感染.笔记!我更改了格式,就像我的标题形式一样.我的其余功能(读取csv文件中的行)受到我的标头的影响,并返回到不希望出现的错误.希望任何人都能救我到我的错误之岛.

But after successfully added my header like this: "EmployeeCode","Date","Time","Type","Remarks" The flow of codes are infected by this type of field in the header. Note! I change the format just like what my header form. The rest of my function that reading the line in csv file is affected by my header.It return to undesirable error. Hope that anyone can rescue me to my Island of error.

推荐答案

您可以将所有StreamWriter代码替换为更简洁的

You can substitute all that StreamWriter code with a more concise File.AppendAllText and replace your string concatenations with an interpolated string.
These changes will result in a more clean InsertEntry method

public bool InsertEntry(string idnumber, string date, string time, string type, string remarks)
{
    try
    {
        string line = $"\"{idnumber}\",\"{date}\",\"{time}\",\"{type}\",\"{remarks}\"{Environment.NewLine}";
        File.AppendAllText(data_path, line);
        return true;
    }
    catch (Exception ee)
    {
        string temp = ee.Message;
        return false;
    }
}

当然,button1单击应该只一次写入标题,而不是每次都单击.为了避免这种情况的发生,您需要在写入标题之前检查文件是否存在.

Of course, the button1 click should write the header just one time, not everytime it is clicked. To avoid this from happening you need to check if the file exists or not before writing the header.

String newFileName = @"E:\BR_AttendanceDownloadLogs" + 
                     DateTime.Today.ToString("yyyyMMdd") + ".csv");
if(!File.Exists(newFileName))
{
    string clientHeader = $"\"EmployeeCode\",\"Date\",\"Time\",\"Type\",\"Remarks\"{Environment.NewLine}";
    File.WriteAllText(newFileName, clientHeader);
}

这篇关于使用C#将标头添加到CSV文件程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆