Archived Forum Post

Index of archived forum posts

Question:

CSV Output Strings with Double Quotes

Jan 06 '16 at 04:24

Hi Forum,

when creating a csv-file, how can I put a single double quote around a Value?

Output shoud be like this: "Text";23,5

When I use SetCell 0, 0, "Text" the result is Text;23,5

When I use SetCell 0, 0, Chr$(35) + "Text" + Chr$(35) the result is """Text""";23,5

Thanks Christian


Accepted Answer

I think Chilkat would have to add a feature for you to be able to always put double-quotes around values. If you have an active support subscription, then perhaps you could fire off a request via email.

A possible workaround would be to pass all of your text values with a different character that won't ever appear in your data (for example, Chr$(1)), then at the end replace all occurrences of that character with double quotes. Here's a simple VB6 example:

   Dim lo_Csv As New Chilkat_v9_5_0.ChilkatCsv

lo_Csv.SetCell 0, 0, Chr$(1) & "Test" & Chr$(1)
   lo_Csv.SetCell 0, 1, 123

Debug.Print Replace$(lo_Csv.SaveToString, Chr$(1), """")

If there's a chance that your data could include reserved characters (which would cause the library to automatically enclose the field value in double-quotes) then you will need to first test that the field value doesn't have a reserved character (and if so don't pad the value with the Chr$(1)).


Answer

jbpro, thanks, that's it.

strTemp = Replace$(objCsv.SaveToString, Chr$(1), """") Call objCsv.LoadFromString(strTemp) objCsv.SaveFile ("E:Test.csv")

The LoadFromString doesn't remove the double quotes, output is exactly what i wanted. "Text 0";23,5


Answer

Try to use '"Text"'


Answer

The library automatically inserts double-quotes as necessary if the passed string includes any reserved characters (double-quotes, newlines, delimiter character). If the passed string doesn't contain any reserved characters, then the double-quotes aren't needed around the resultant field value.

See the relevant portion of the documentation here:

SetCell(ByVal row As Long, ByVal col As Long, ByVal content As String) As Long

Sets the contents for a single cell in the CSV. The content may include any characters including CRLF's, double-quotes, and the delimiter character. The Save methods automatically double-quote fields with special chars when saving. The Load methods automatically parse double-quoted and/or escaped fields correctly when loading.

Returns 1 for success, 0 for failure.


Answer

Thanks so far, but that doesen't help me.

I've read the documentation and I know that strings not containing special characters doesn't have to be quoted. But I have to "produce" a given format, the other side want's to have strings in double qoutes.

If there's no possibility to give out "normal" strings in double qoutes, I cannot use the library.