简体   繁体   中英

columns combining when c# appends to csv

We've created a desktop timer that our users are using to track their daily tasks and projects. It outputs data to a .csv file when they close the application. Occasionally they are going to need to manually update the csv file to either take time off or add time. When they have been doing this in the current state after they save all the columns are combined into column A. I am unclear on what is causing this and tried to research encoding but couldn't find anything I could relate to this scenario.

Full form1.cs: full form1.cs code

Code related to csv:

    //event handler for the closing event -- the output dump is here so the timer can be used all day and captures even on an accidetnal close
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

        //if the file exists we don't need to write the headers so we can skip the additional line write and just append the data created
        if (File.Exists(dskPath + "\\" + agentName + ".csv"))
        {
            using (var file = new StreamWriter(dskPath+"\\"  + agentName + ".csv", true, Encoding.UTF8))
            {

                foreach (var data in TimerData)
                {
                    file.WriteLine(string.Join(",", data.agentID, data.agentStatus, data.statusTime, data.startTime, data.currentDate, data.hydraTask, data.clientname, data.publishdate, data.notes));
                }
                file.Close();
                notifyIcon1.Icon = null;
                MessageBox.Show("The file for " + agentName + " has been written to G:\\Communicator Ops\\Population Health\\Pop Process\\Time Tracking\\User Data.", "Application Closed");
            }
        }
        //if the file hasn't been created before we can drop the headers in for the columns
        else
        {
            using (var file = new StreamWriter(dskPath +"\\"+ agentName + ".csv", true, Encoding.UTF8))
            {
                file.WriteLine(string.Join(",", "Agent ID", "Agent Status", "Status Time", "Start Time", "Current Date", "hydra task number", "Client Name", "Publish Date", "Notes"));
                foreach (var data in TimerData)
                {
                    file.WriteLine(string.Join(",", data.agentID, data.agentStatus, data.statusTime, data.startTime, data.currentDate, data.hydraTask, data.clientname, data.publishdate, data.notes));
                }
                file.Close();
                notifyIcon1.Icon = null;
                MessageBox.Show("The file for " + agentName + " has been written to your desktop.","Application Closed");
            }
        }
    }

I have a theory about what might be occurring. The C# code looks OK. I think the users are opening the file in a way you were not expecting. I think they are doing the following:

1) Opening Excel and then navigating to the file location. This will cause this dialog to open: 在此处输入图片说明

2) They hit Next and get to step 2. 在此处输入图片说明

At this step they may be leaving Tab selected as the delimiter which is NOT correct. This is where they may be causing your problem.

They hit Next again

3) 在此处输入图片说明

Finally they hit Finish and everything ends up in column A. 在此处输入图片说明

If I double click on the desktop icon it seems to work OK. If I select comma as the delimiter, and uncheck Tab, that worked too.

That is my guess. Hopefully this will help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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