3

I am using FileHelpers to write DataTable content into CSV file.

Because of huge number of records in DataTable I chose to dump the result set as it is in DataTable into CSV file like below

CommonEngine.DataTableToCSV(dt, filename)

And the CSV has sucessfully written with 2 million records having the size of 150MB.

But I wanted to add the filed header at first line of this CSV file.

Is there a way FileHelper will allow to write the header using CommonEngine.DataTableToCSV?

vijay
  • 635
  • 2
  • 13
  • 26

2 Answers2

7

You must use engine.HeaderText = engine.GetFileHeader() before you call WriteFile

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Stefan Z Camilleri
  • 4,016
  • 1
  • 32
  • 42
0

Looking at the source code, it looks like there is no way of setting the header line. However, it is easy to copy the code and write your own replacement. Something like this:

public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options, string headerLine)
{
    using (StreamWriter writer = new StreamWriter(filename, false, options.Encoding, 102400))
    {
        // output header
        writer.Write(headerLine);
        writer.Write(StringHelper.NewLine);

        foreach (DataRow row in dt.Rows)
        {
            object[] itemArray = row.ItemArray;
            for (int i = 0; i < itemArray.Length; i++)
            {
                if (i > 0)
                {
                    writer.Write(options.Delimiter);
                }
                writer.Write(options.ValueToString(itemArray[i]));
            }
            writer.Write(StringHelper.NewLine);
        }
        writer.Close();
    }
}
shamp00
  • 11,106
  • 4
  • 38
  • 81
  • Thanks much for reply, Filehelpers dll already in used in other modules, and some of them went to production. We are not permissible to add changes. If the filehelpers doesn't allow this, Is there any other way of achieving the same using C# native file read/write methods. My concern is I am not sure which it the effective way to do it without overloading memory. – vijay Jun 30 '14 at 11:08
  • 1
    You don't have to modify the FileHelpers source. Just add a new static class called `MyCommonEngine` with the above method and call it instead. It is using exactly the same source code as FileHelpers so the memory use should be the same. – shamp00 Jun 30 '14 at 11:42
  • This is really very well idea, but my bad, options.ValueToString is an internal method, like this most of the classes and methods we need are having access limitations. To implement your code snippet, I may have to end up copy number of classes of filehelpers. In other way, I use filehelpers to write an entire file, then I will add the header opening the file in an append mode, how do I append the header just using C# file I/O methods? – vijay Jun 30 '14 at 13:22
  • 1
    You can find an efficient file prepend method [here](https://stackoverflow.com/questions/8235839/add-a-single-line-to-the-top-of-text-file) – shamp00 Jun 30 '14 at 15:53