Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:09:11+00:00 2026-06-10T16:09:11+00:00

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

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:09:13+00:00Added an answer on June 10, 2026 at 4:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the code below to split up a fullname from a .csv file
I am writing a Java app to export data from Oracle to csv file
I'm running several scripts which are writing and reading from the same CSV file
I'm writing application for downloading CSV file from web and inserting data into table
Hi I'm using the following code to generate a CSV file containing data from
Currently I'm writing a method to read data from a CSV file and import
I am writing a Django view which takes an attached csv file and reads
I'm writing some java code that's supposed to parse csv files with different column
I have a list of string that I am writing out to a CSV
I've been asked to output a CSV file from a view in MySQL. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.