0

I would like to export my class in .csv file, i follow this comment : https://stackoverflow.com/a/38088636/10152334

but when i export (in Buses_Data.Buses_List), i have wrong data output, all data are on same line :

enter image description here

public void ExportToCSV(Buses_Data classToExport, string filepath)
    {
        try
        {
            if (File.Exists(filepath))
            {
                Console.WriteLine("Le fichier existe");
                System.IO.File.Delete(filepath);
                Console.WriteLine("l'ancien fichier à été supprimé");
            }

            Console.WriteLine("Export :...");


            using (StreamWriter sw = new StreamWriter(filepath))
            using (CsvWriter cw = new CsvWriter(sw))
            {
                cw.WriteHeader<Bus>();

                foreach (Bus emp in classToExport.Buses_List)
                {
                    cw.WriteRecord<Bus>(emp);
                }
            }

            Console.WriteLine("Export CSV: OK");
        }
        catch (Exception i)
        {
            Console.WriteLine("Error ! " + i);
        }
    }

I don't know why the code doesn't work

Edit : Wrong output: Wrong output

Right output (Data not corresponding) Right output (Data not corresponding)

Edit 2 : I tried this comment : Enforce LF line endings with CsvHelper

using (StreamWriter sw = new StreamWriter(filepath))

            using (CsvWriter cw = new CsvWriter(sw))
            {
                cw.WriteHeader<Bus>();

                foreach (Bus emp in classToExport.Buses_List)
                {

                    cw.WriteRecord<Bus>(emp);
                    sw.NewLine = "\n";
                    cw.NextRecord();
                }
            }

I have better result but i Bus N°1 is on first line, not in second line

enter image description here

Solution :

using (StreamWriter sw = new StreamWriter(filepath))
            using (CsvWriter cw = new CsvWriter(sw))
            {
                cw.WriteHeader<Bus>();
                cw.NextRecord();
                foreach (Bus emp in classToExport.Buses_List)
                {

                    cw.WriteRecord<Bus>(emp);
                    //sw.NewLine = "\n";
                    cw.NextRecord();
                }
            }
9
  • CSV files is just text files with delimiters. Did you check the actual file? What does it contain? The screenshot posted here doesn't help at all Commented Aug 12, 2019 at 14:21
  • 2
    It could be that whatever program you used to import the CSV data didn't recognize the newline and ended up showing everything in the same line. When you double-click on a CSV file and expect it to open in Excel for example, Excel imports the data using the system's defaults for newlines, decimal and list separators. You'd have to go to Data > From Text to specify the delimiters you want Commented Aug 12, 2019 at 14:23
  • If you want to create files for Excel, why not create actual Excel files with a library like Epplus? Commented Aug 12, 2019 at 14:25
  • @PanagiotisKanavos I use Microsoft Office enterprise 2019, but it's work with other file Commented Aug 12, 2019 at 14:25
  • 2
    @Bensuperpc add a NextRecord() after WriteHeader too. That's what CsvWriter itself does Commented Aug 12, 2019 at 15:02

2 Answers 2

2

Based on answer of GitHub issue, written by Josh Close:

You need to call NextRecord() when you're done writing the header. This is so you can write more fields manually before or after.

I tried it, it works well.

Sign up to request clarification or add additional context in comments.

1 Comment

Good pointing this out also. His output seems to indicate he'd need this both places.
2

Have you tried CsvWriter.NextRecord?

foreach (Bus emp in classToExport.Buses_List)
{
    cw.WriteRecord<Bus>(emp);
    cw.NextRecord();
}

4 Comments

That's not needed according to the doc example
Yes if OP changes their pattern to write all records. I'm not sure if there is some other code they want to fire between each record write (logging, notification, etc.) so I didn't want to presume a refactor would be the answer. The method is not depreciated, so I assume it's clearly part of the intended API use: github.com/JoshClose/CsvHelper/blob/master/src/CsvHelper/…
You're right. I keep forgetting how .... idiosyncratic the examples are, and that most of the time one has to check the tests, not the docs.
I tried (with other solution), I have better results (see my second edit)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.