简体   繁体   中英

Streamwriter adding in extra line breaks randomly when exporting from visual studio to csv c#

I have been trying for a while now to export a data table to csv in visual studio after datalogging.

For some reason I get weird results when importing into excel whereas notepad is fine. It seems line breaks are being added in but at random points within the data.

I have battling this for a while now so any help is appreciated!

Excel Version

Notepad Version

void SaveAllData()
    {
        {
            saveFileDialog1.InitialDirectory = "C"; // open save file window
            saveFileDialog1.Title = "Save as CSV File"; // promt save as csv
            saveFileDialog1.FileName = DateTime.Now.ToString("dd.MM.yy_HH.mm");
            saveFileDialog1.Filter = "CSV File | *.csv"; // csv format

            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel) // if save dialog opens 
            {
                string value = "";
                DataGridViewRow dr = new DataGridViewRow();
                StreamWriter swOut = new StreamWriter(saveFileDialog1.FileName, true, Encoding.ASCII);

                //write header rows to csv
                for (int i = 0; i <= IncomingDataTable.Columns.Count - 1; i++)
                {
                    if (i > 0)
                    {
                        swOut.Write(",");
                    }
                    swOut.Write(IncomingDataTable.Columns[i].HeaderText);
                }

                swOut.WriteLine();

                //write DataGridView rows to csv
                for (int j = 0; j <= IncomingDataTable.Rows.Count - 1; j++)
                {
                    if (j > 0)
                    {
                        swOut.WriteLine();
                    }
                    dr = IncomingDataTable.Rows[j];

                    for (int i = 0; i <= IncomingDataTable.Columns.Count - 1; i++)
                    {
                        if (i > 0)
                        {
                            swOut.Write(",");

                       }

                        value = dr.Cells[i].Value.ToString();
                        swOut.Write(value);

                    }
                }
                swOut.Close();
            }

    MessageBox.Show("Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); // alert user file has saved



            serialPort1.Close(); // close serial port
            Setup_Page f1 = new Setup_Page(); // open setup page
            this.Hide();
            f1.ShowDialog();
            Close(); // close this page
        }
    }

If your input data contains linefeeds and you output them, some applications will treat them as line breaks but some won't. Notepad does not, so it looks like a single line to Notepad. Excel recognizes the linefeeds and begins a new row. Open the CSV file in WordPad instead of Notepad; WordPad will recognize linefeeds whereas they are invisible in Notepad.

The simple fix is to call String.Trim on your input data before writing it. However, as Magnus mentioned, libraries are your friend. If you had used a good one, this may not have been a problem in the first place, as a well-written library would have handled embedded newlines in the data.

You may say that you can trim newlines. Well, what if a newline is in the middle of your data? Trim won't handle that. There won't be any newlines? What about commas? There won't be any commas, because the data is only numeric? What if it is a decimal, and the computer that produced it is in France, or numerous other countries that use the comma as the decimal separator? What about quotes in the data? A library may have already handled all these cases. (The comma decimal separator may still be a problem to whoever uses the output, but at least the CSV will be well-formed.)

Note, though, that CSV is not a well-defined standard. Different applications and libraries use different strategies to handle the above cases, so if your library didn't use the same format as Excel, it still might not load correctly.

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