Wesley Skeen
Wesley Skeen

Reputation: 8285

Export a string to csv

I'm new to exporting to csv. I have a string that is generated from a gridview (str = coups.Cells[0].Text + " -- " + coups.Cells[1].Text). I would like to know how to export this to a csv file.

How do i generate headers in the csv file and then match them up.

I'm new to exporting to csv.

Thanks to all

Upvotes: 0

Views: 2013

Answers (5)

NaveenBhat
NaveenBhat

Reputation: 3318

How do i generate headers in the csv file and then match them up.

CSV Means Comma Separated Value, here each values is Separated with ,. There is nothing special as headers. However, You can consider the First Row as a header.

Upvotes: 0

Evan Larsen
Evan Larsen

Reputation: 9965

Heres a method I wrote a while back that you can pass a string into and it formats it for CSV... just seperate each string by a ; and it will be in a seperate column. Do a new line (hard return) to signify another row. Sorry this in VB.NET

Private Function FormatStringForCsvFile(ByVal strCSV As String) As String
        Dim addDoubleQuotes As Boolean = False
        Dim strReturn As String = strCSV
        If strReturn IsNot Nothing AndAlso strReturn.Length > 0 Then
            'if the string contains any double quotes then we need to change those double quotes to 2x double quotes
            If strReturn.Contains("""") Then
                strReturn = strReturn.Replace("""", """""")
                addDoubleQuotes = True
            End If
            'if the string contains a comma, any spaces before or after, 
            'any 2x double quotes, or a new line then we need to enclose it in double quotes
            If strReturn.Contains(",") Or Char.IsWhiteSpace(strReturn(0)) _
                Or Char.IsWhiteSpace(strReturn(strReturn.Length - 1)) _
                Or addDoubleQuotes Or strReturn.Contains(vbCrLf) Then
                strReturn = """" & strReturn & """"
            End If
        End If

        Return strReturn

    End Function

This method is used to format the string which you want to seperate with the ; deliminator because your string which your seperating does not always conform to csv rules. If there is a ; or " or hard return in your string.. well you need to enclose that in double quotes.. This will let the program which is rendering the csv that the ; " are all part of the string and not signifying a deliminator

Upvotes: 1

agent-j
agent-j

Reputation: 27923

What's wrong with this?

TextWriter x = File.OpenWrite ("my.csv", ....);

x.WriteLine("Column1,Column2"); // header
x.WriteLine(coups.Cells[0].Text + "," + coups.Cells[1].Text);

The column delimiter is a comma. This is sufficient:

Column1,Column2
data,data2
data,data2

They don't have to line up visually like this:.

Column1,    Column2
data,       data2
data,       data2

Edit: In the United States, Comma means , and semicolon means ;. You'll want to pick the delimiter that your software uses.

Upvotes: 1

Philipp Schmid
Philipp Schmid

Reputation: 5828

CSV stands for *C*omma *S*eparated *V*alues. So any string concatenation method will work. The only tricky part is that if a value contains a comma, then the entire value needs to be surrounded by quotes like this:

"Mouse, Mickey", Disney Land, USA

Upvotes: 0

RiaD
RiaD

Reputation: 47620

Csv is just list of string with ; between cells

String with ; you should have in quotes

Example

Language; Country
RU; Russia
EN; UK
"string with ; in ";I like StackOverFlow

Upvotes: 0

Related Questions