• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

Commas in A Streamed CSV

I am streaming a CSV file to the browser.

The problems that I'm presented with are the newline char, and the comma char - both for fairly obvious reasons.

I simply stripped the new line, but I don't want to remove the comma completly. One solution I attempted is to place quotes (both single and double) around the strings within the csv. The quotes are simply interpreted as part of the file, and not something denoting the start and end of a string, so this did not work (I am using a stringbuilder to concat my whole csv together before streaming if that is relevant). The other solution was to replace the comma character with an escaped comma ('\,'), but it's not recognized by VS as a valid escape sequence, and therefore craps out on compile.

Any ideas on how to handle this?

Thanks,
G
0
GENTP
Asked:
GENTP
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
.NET version?  2002, 2003, or 2005?

Bob
0
 
GENTPAuthor Commented:
2002. Framework V 1.1.

Thanks,
G
0
 
Bob LearnedCommented:
Where is the data coming from?  A DataTable?

Bob
0
 
GENTPAuthor Commented:
It's coming from looping through a SqlDataReader. I could switch the method I use if needed.

G
0
 
Bob LearnedCommented:
This is what I use to stream CSV:

Public Class Export

  Private response As HttpResponse

  ' Exports data to a custom format
  Public Sub ExportCSV(ByVal data As DataTable, ByVal fileName As String)

    response = HttpContext.Current.Response

    response.Clear()
    response.ClearHeaders()
    response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
    response.Expires = 1
    response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate)
    response.Cache.SetValidUntilExpires(False)
    response.CacheControl = "private"
    response.AddHeader("pragma", "cache")

    ' Add the header that specifies the default filename
    ' for the Download/SaveAs dialog
    response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)

    Dim delimiter As String = String.Empty

    response.ContentType = "text/csv"

    Dim exportContent As String = ConvertDataTableToString(data, delimiter)

    If exportContent.Length <= 0 Then
      exportContent = "Error converting data"
    End If

    response.AddHeader("Content-Length", Encoding.UTF8.GetByteCount(exportContent))
    response.BinaryWrite(Encoding.UTF8.GetBytes(exportContent))

    ' Stop execution of the current page.
    response.End()

  End Sub  'ExportCSV

  Public Function ConvertDataTableToString(ByVal sourceData As DataTable, Optional ByVal Delimiter As String = Nothing, Optional ByVal Separator As String = ",") As String

    Dim resultBuilder As StringBuilder
    resultBuilder = New StringBuilder
    resultBuilder.Length = 0

    resultBuilder.Append(ControlChars.Quote)

    ' Add column headers.
    For Each sourceColumn As DataColumn In sourceData.Columns

      If Not Delimiter Is Nothing AndAlso (Delimiter.Trim.Length > 0) Then
        resultBuilder.Append(Delimiter)
      End If

      resultBuilder.Append(sourceColumn.ColumnName)

      If Not Delimiter Is Nothing AndAlso (Delimiter.Trim.Length > 0) Then
        resultBuilder.Append(Delimiter)
      End If

      resultBuilder.Append(Separator)

    Next sourceColumn

    If resultBuilder.Length > Separator.Trim.Length Then
      resultBuilder.Length = resultBuilder.Length - Separator.Trim.Length
    End If

    resultBuilder.Append(ControlChars.Quote)
    resultBuilder.Append(ControlChars.CrLf)

    ' Add data rows.
    For Each rowCurrent As DataRow In sourceData.Rows

      resultBuilder.Append(ControlChars.Quote)

      For Each columnCurrent As DataColumn In sourceData.Columns

        If Not Delimiter Is Nothing AndAlso (Delimiter.Trim.Length > 0) Then
          resultBuilder.Append(Delimiter)
        End If

        resultBuilder.Append(rowCurrent(columnCurrent.ColumnName))

        If Not Delimiter Is Nothing AndAlso (Delimiter.Trim.Length > 0) Then
          resultBuilder.Append(Delimiter)
        End If
        resultBuilder.Append(Separator)

      Next columnCurrent

      resultBuilder.Length = resultBuilder.Length - 1
      resultBuilder.Append(ControlChars.Quote)
      resultBuilder.Append(ControlChars.CrLf)

    Next rowCurrent

    Return resultBuilder.ToString()

  End Function

End Class

Bob
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now