Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

When string in a cell has double quotes those appear in Excel after export. How do I remove them?

$
0
0
I need help to fix an issue where when data from web application is exported to Excel by clicking a button export to excel, if the data in a cell contains double quotes, that data should be displayed without the double quotes visible.

Previously I made a change to the application code in VB so that the output exports text fields with formulas (="") to force Excel to treat those values as a string. This has been working except some instances where the output actually displays the formula characters (="") within the cell as text, rather than as hidden formulas. It appears when a cell contains text with an actual double quotes that is when after export to Excel is done, those quotes appear in Excel. I need help to figure out if there is a way to suppress those.

For example. A cell with the following data 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.

When this is exported to Excel the data is displayed as follows ="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. I do not want to quotes to appear in Excel.

On the other hand, a cell with the following data Maintain Victim Classification Types. when this is exported to Excel there are no visible quotes. It displays as Maintain Victim Classification Types.

Here is my VB code that needed changing

ProtectedSub WriteToExcelFile(dt As DataTable)'This method exports the resulting query datatable to an instance of Excel using StringWriterIfNot dt IsNothingThenDim sw AsNew StringWriter()'Loop through the column names and output those firstForEach datacol As DataColumn In dt.Columns
            sw.Write(datacol.ColumnName + vbTab)NextDim row As DataRow'Loop through the datatable's rowsForEach row In dt.Rows'Newline between the previous row and the next row
            sw.Write(vbNewLine)Dim column AsNew DataColumn()'Loop through each column and write the cell the the stringwriterForEach column In dt.Columns'If the cell isn't empty write it, else write an empty cellIfNot row(column.ColumnName) IsNothingThen
					sw.Write("="""& row(column).ToString().Trim() & """"& vbTab)Else
                        sw.Write(String.Empty + vbTab)EndIfNext columnNext 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()EndIfEndSub

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>