-2

I have a 'translate content' application. Basically, a user uses Summernote text editor to put content (HTML) into the system. They can then request a translation by downloading a csv file with all the information in it to send to the translator. When we get it back from the translator, we will upload the csv file that contains the translations.

Issue 1: Some of the columns are content based and have commas in them. The same content is also in HTML format. So you may have:

<p style: "color: blue;"> I am a test, and I like to cause problems </p>

Issue 2: When I re-import the file, it delimits by semi-colon. So everything in the in-line styling is broken up.

The Code to make the CSV:

Response.ContentType = "text/plain";
Response.AddHeader("content-disposition", "attachment;filename=" + downloadName);
Response.Clear();
   using (StreamWriter contentWriter = new StreamWriter(Response.OutputStream, Encoding.UTF8))
   {
       contentWriter.WriteLine("Application ID, English ID, Title, Application, Content, Translate To, TRANSLATE HERE, Requested By, Request Date");

                foreach (translation_contents content in toBeTranslatedContents)
                {
                    Guid parentKey = content.translation_contentid;
                    string title = string.Format(" \"{0} \" ", content.ContentTitle);
                    string englishContent = string.Format(" \"{0} \" ", content.Content);


                    contentWriter.WriteLine(applicationKey + "," + parentKey + "," + title + "," + applicationName + "," + englishContent + "," + newLanguage + "," + englishContent + "," + requestedBy + "," + requestDate);
                }
     }

      Response.End();

What I have tried:

string englishContent = string.Format(" \"{0} \" ", content.Content);

Everything said that this should work. It does, in fact, add quotes around the content. However, it still delimits by the commas. So I'll get two columns such as

col 1: "I am a test
col 2: and I like to cause problems"

Where the quotes are there, but it's still broken up. I also tried:

string englishContent = content.Content.Replace(",", "&apos;");

This works very well on export and can still be understood and rendered by SummerNote Text Editor. The issue is that when I re-import it, the semi-colons are wreaking havoc and breaking the content up into 5-10 columns rather than just the one full piece of content.

Code To Import:

using (StreamReader fileReader = new StreamReader(path))
{
    List<string> listA = new List<string>();
    while (!fileReader.EndOfStream)
    {
        var line = fileReader.ReadLine();
        var values = line.Split(';');

         listA.Add(values[0]);
     }
  }

Thoughts on how to just keep content as a single block and not break it up or delimit it in anyway?

Thank you in advance!

HumanHickory
  • 464
  • 1
  • 6
  • 19
  • 3
    No it's not delimiting anything. `StreamWriter` writes, it doesn't delimit anything. It doesn't have columns. It won't write anything by itself, it does exactly what you tell it to do. The *columns* are produces by `var values = line.Split(',');` in the reading code. That line has no idea that some fields are quoted. – Panagiotis Kanavos Jun 18 '19 at 15:43
  • 2
    CSVs aren't as simple as they appear. Fields *can* contain separators and newlines. Which means that `fileReader.ReadLine();` can also be a problem. If you intend to handle commas and newlines, you should probably use a library like [CsvHelper](https://joshclose.github.io/CsvHelper/) to properly quote, export and import files with different field or row separators – Panagiotis Kanavos Jun 18 '19 at 15:49
  • So then what in the 'code to make the CSV' is telling it to delimit by comma or is it Excel that is delimiting it? Either way, what would you recommend as a solution? EDIT: I Saw your second post. Ignore me asking for the solution you'd recommend. – HumanHickory Jun 18 '19 at 16:01
  • `what in the 'code to make the CSV' is telling it to delimit by comma` *nothing*. The bug is in the code that *reads* the text without checking for quoted fields. – Panagiotis Kanavos Jun 19 '19 at 07:41

1 Answers1

0

The answer by Panagiotis Kanavos solved my issue. Using CSVhelper, I was able to export and import CSVs with ease.

HumanHickory
  • 464
  • 1
  • 6
  • 19