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 ?

2
  • 1
    That will defeat the puspose of a CSV file. You can not do that using a csv file. Commented Aug 22, 2012 at 4:59
  • possible duplicate of make color cell in csv file for Excel Commented Aug 22, 2012 at 5:05

4 Answers 4

25

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.

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

3 Comments

Why? If their question is »How can I do that« I think the answer »You cannot« is one.
@Joey are you sure ? I can not do that ?
Yes, I am sure. Read the linked reference, which is sort of the de-facto CSV standard. Do you see anything about formatting in there? I don't.
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

1 Comment

That I am trying and is lot more complicated.
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();
}

1 Comment

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 ..?
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).

1 Comment

RTF is a completely different format, though, representing rich text and not tabular data.

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.