5

I am writing csv file from Datatable. Check my code below

      public static void SaveDataTableToCsvFile(string AbsolutePathAndFileName, DataTable TheDataTable, params string[] Options)
    {
        //variables
        string separator;
        if (Options.Length > 0)
        {
            separator = Options[0];
        }
        else
        {
            separator = ""; //default
        }
        string quote = "";

        FileInfo info = new FileInfo(AbsolutePathAndFileName);

        if (IsFileLocked(info))
        {
            MessageBox.Show("File is in use, please close the file");
            return;
        }
        //create CSV file
        StreamWriter sw = new StreamWriter(AbsolutePathAndFileName);

        //write header line
        int iColCount = TheDataTable.Columns.Count;
        for (int i = 0; i < iColCount; i++)
        {
            sw.Write(TheDataTable.Columns[i]);
            if (i < iColCount - 1)
            {
                sw.Write(separator);
            }
        }
        sw.Write(sw.NewLine);

        //write rows
        foreach (DataRow dr in TheDataTable.Rows)
        {
            for (int i = 0; i < iColCount; i++)
            {
                if (!Convert.IsDBNull(dr[i]))
                {
                    string data = dr[i].ToString();
                    data = data.Replace("\"", "\\\"").Replace(",", " ");
                    sw.Write(quote + data + quote);
                }
                if (i < iColCount - 1)
                {
                    sw.Write(separator);
                }
            }
            sw.Write(sw.NewLine);

        }
        sw.Close();
    }

Code works for me ,but I need to add color code in some cells of csv.

How can I do that ?

Lajja Thaker
  • 2,031
  • 8
  • 33
  • 53

4 Answers4

24

CSV is a pure data format without any formatting. It's a plain text file after all. So no, there is no way of adding colour.

Community
  • 1
  • 1
Joey
  • 344,408
  • 85
  • 689
  • 683
2

You might want to output an .xls (or equivalent) instead of a .csv using some external utility Or convert the csv to .xls in order to have color coding even possible

rshetye
  • 667
  • 1
  • 8
  • 21
1

Joey is absolutely right.

But if your situation allows you to output an XLSX instead of a CSV, then EPPlus might be the solution for you.

e.g.

using (ExcelPackage ep = new ExcelPackage(AbsolutePathAndFileName))
{
    ExcelWorksheet worksheet = ep.Workbook.Worksheets.Add("Worksheet1");
    worksheet.Cells["A1"].LoadFromDataTable(TheDataTable, true); 
    worksheet.Cells["F4"].BackgroundColor.SetColor(Color.Red);
    ep.Save();
}
Merenzo
  • 5,326
  • 4
  • 31
  • 46
  • so if you have 4 Datatables and need to add all of them to a single worksheet with 2 rows separating each of the 4 datatables would `EPPlus` still handle this I notice the .`Save()` I am use to using `Response.Headers` to export and allow the user to save or open the excel from the web does EPPlus support his ..? – MethodMan Dec 24 '15 at 00:29
0

A CSV (comma-separated values) file is a text file. There is no way to add color too the file without changing it to another file format (such as RTF).

IBam
  • 10,114
  • 1
  • 24
  • 36
Craig T
  • 2,761
  • 5
  • 25
  • 33
  • RTF is a completely different format, though, representing rich text and not tabular data. – Joey Aug 22 '12 at 05:44