Results 1 to 3 of 3

Thread: How do I remove double quotes when data is exported to Excel from web application?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    How do I remove double quotes when data is exported to Excel from web application?

    I need help to fix an issue where when string have a double quote in the result and that data is exported to Excel from web application by clicking a button, the data exported to Excel shows up with double visible. I do not want this to show. On the other hand, if the data(string) has no double quotes, the display in Excel is okay
    Here is an example. A cell with the following data <b>Allows the user the abilities to Add, View, Modify and Delete Notes on the Notes Tab of the Case Record. "View" allows the user to view the Notes Tab of the Case Record.</b>
    When this is exported to Excel the data is displayed as follows <b>="Allows the user the abilities to Add, View, Modify and Delete Notes on the Notes Tab of the Case Record. "View" allows the user to view the Notes Tab of the Case Record.</b> I do not want to quotes to appear in Excel.
    On the other hand, a cell with the following data <b>Maintain Victim Classification Types.</b> when this is exported to Excel there are no visible quotes. It displays as <b>Maintain Victim Classification Types.</b>

    Here is my VB code that needed changing

    Code:
    Protected Sub WriteToExcelFile(dt As DataTable)
        'This method exports the resulting query datatable to an instance of Excel using StringWriter
        If Not dt Is Nothing Then
            Dim sw As New StringWriter()
            'Loop through the column names and output those first
            For Each datacol As DataColumn In dt.Columns
                sw.Write(datacol.ColumnName + vbTab)
            Next
            Dim row As DataRow
            'Loop through the datatable's rows
            For Each row In dt.Rows
                'Newline between the previous row and the next row
                sw.Write(vbNewLine)
                Dim column As New DataColumn()
                'Loop through each column and write the cell the the stringwriter
                For Each column In dt.Columns
                    'If the cell isn't empty write it, else write an empty cell
                    If Not row(column.ColumnName) Is Nothing Then
    					sw.Write("=""" & row(column).ToString().Trim() & """" & vbTab)
                        Else
                            sw.Write(String.Empty + vbTab)
                        End If
                    Next column
                Next row
                'create an instance of Excel and write the data
                Response.Clear()
                Response.ContentType = "application/vnd.ms-excel"
                Response.AddHeader("Content-Disposition", "attachment;filename=GridViewExport.xls")
                Response.Output.Write(sw.ToString())
                Response.Flush()
                System.Web.HttpContext.Current.Response.Flush()
                System.Web.HttpContext.Current.Response.SuppressContent = True
                System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()
            End If
    End Sub

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: How do I remove double quotes when data is exported to Excel from web application

    Regular expressions is one way

    Code:
    Dim line = "1.5"" something ""hello""    ""cool""car"" single sentence with out quotes."
    Dim regex As New Regex("""([^\\""\r\n]*(?:\\.[^\\""\r\n]*)*)""")
    Console.WriteLine(line)
    Console.WriteLine(regex.Replace(line, "$1"))

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How do I remove double quotes when data is exported to Excel from web application

    Code:
    sw.Write("= " & row(column).ToString().Trim() & vbTab)
    ???

    Why put extra quotation marks in if you don't want quotation marks?
    if row(column).ToString() contains quotes...

    Code:
    sw.Write("= " & row(column).ToString().Replace(chr(34).ToString(), "").Trim() & vbTab)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width