Write csv file with color code
c#, sql
Solution
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.
Problem
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 ?