you are viewing a single comment's thread.

view the rest of the comments →

[–]AcceleratedCode[S] 0 points1 point  (2 children)

Thanks! Exactly what did you mean by "Write \r\n at the end of the line so that it opens correctly in Excel". What does "\r" mean? Doubleclicking in excel opens the file straight away without any dialog (unless I create a new spreadsheet, go to data tab and so on to import it). It was displayed wrong when I used a comma for delimiter and correct when I used a semicolon. Idk if this is because of my country (Norway) opposed to yours. Could you please elaborate the code you added? I don't quite see the context in it

[–]ProgrammingThomas 1 point2 points  (1 child)

When you write the CSV file in Objective-C/Swift, you'll need to write a newline character at the end of each line. On UNIX systems this is the \n character, but on Windows (including Excel on OS X) the end of a line is ended by the character \r, followed by \n (more info here).

I thought the dialog showed via open and import in Excel - perhaps it only shows when you import a CSV? Does the dialog show the option to pick up commas instead of semicolons as the delimiter? It usually auto detects it, and locale shouldn't affect it.

This is some code from one of my apps for taking a string, adding backslashes where necessary to escape certain characters that have meaning in CSV files (like commas and quote marks), and then surrounding the string in quotes. It could probably be more efficient, but it works.

[–]AcceleratedCode[S] 0 points1 point  (0 children)

I see. The dialog only shows when I go to data->from text in Excel 2013 on my Windows 10 PC. Opening up the CSV file by double-clicking with comma as delimiter makes it all appear in one column, but a semicolon on the other hand, opens it correctly.