Reply to thread

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


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


Continue reading...


Back
Top