Medic3000
Medic3000

Reputation: 786

How can I format text when writing to CSV file

I have a chunk of code responsible for writing a .CSV file, BUT the formatting is horrid. I'm wondering if there is a command or string I can send to the CSV to signal that it should Bold the first row of text and center text for all cells. Is this possible?

 StreamWriter sw = null;
 try
 {
     this.SetStatus(DataLogUIStrings.strSavingToCSVFile);
     sw = new StreamWriter(exportFileName, this.appendExport);
     //first row of CSV
     sw.WriteLine("Start Time,End Time,Date,JobID,,Alarm Count");
     foreach (SJob job in jobsToExport)
     {
         //rest of CSV rows
         sw.WriteLine(job.GetJobInformation().Replace(',', '\t'));
     }
     this.SetStatus(string.Format(DataLogUIStrings.strJobsExported, jobsToExport.Count));
 }
 catch
 {
  ...
 }

My search has come up with nothing, I was hoping there was some excel formula command so the string could just be "BOLD(Start Time,End Time,...)"; if not let me know any alternates that could get this done w/o opening the excel sheet and manually formatting (as software produce around 24 a day)

Upvotes: 0

Views: 18459

Answers (5)

William Lees
William Lees

Reputation: 1

While you can't add formatting to a csv file, you can save the data as an html table and import into Excel with File/Open. You can use most html formatting including css styles.

Upvotes: 0

Sam Axe
Sam Axe

Reputation: 33738

You are not writing a CSV file. You are converting a comma-separated string into a tab-separated string - and doing it poorly (not respecting escape sequences). So the file you are writing might be a tsv, or perhaps just gibberish.

Second, CSV and TSV are just storage formats. Unlike HTML or XLSX or DOCX, CSV and TSV do not include formatting instructions.

Choose a format that allows for both data storage and formatting.

Upvotes: 0

freshvolk
freshvolk

Reputation: 81

While csv is plain text (so no formatting), there are some pretty good C# excel libraries. If you are going to be viewing/using excel, I would recommend checking some of the Excel export libraries.

Checkout this question/answer for one: https://stackoverflow.com/a/2603625/2435543

Upvotes: 1

catfood
catfood

Reputation: 4331

No. CSV is a way of transmitting plain text. It doesn't have formatting, other than whatever the receiving application chooses to interpret.

Maybe you could write a little VBA app to do an import and massage the results?

Upvotes: 1

8192K
8192K

Reputation: 5290

With CSV there are no options for text formatting. It's just plain text (or values).

If you want formatting with excel cells - save it as xls or xlsx!

Upvotes: 7

Related Questions