Hi,
I have using below code and try to display data like this
Column1 column2
aaa +919874563210
but my data display like this
Column1 column2
HTML
<asp:Button Text="Export" OnClick="ExportCSV" runat="server" />
VB.Net
Protected Sub ExportCSV(sender As Object, e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT 'a,b,c,v' column1,'+919874563210' column2 FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
'Build the CSV file data as a Comma separated string.
Dim csv As String = String.Empty
For Each column As DataColumn In dt.Columns
'Add the Header row for CSV file.
csv += column.ColumnName + ","c
Next
'Add new line.
csv += vbCr & vbLf
For Each row As DataRow In dt.Rows
For Each column As DataColumn In dt.Columns
'Add the Data rows.
csv += AddEscapeSequenceInCsvField(row(column.ColumnName).ToString()) + ","
Next
'Add new line.
csv += vbCr & vbLf
Next
'Download the CSV file.
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv")
Response.Charset = ""
Response.ContentType = "application/text"
Response.Output.Write(csv)
Response.Flush()
Response.End()
End Using
End Using
End Using
End Using
End Sub
Private Function AddEscapeSequenceInCsvField(ValueToEscape As String) As String
If ValueToEscape.Contains(",") Then
Return (Convert.ToString("""") & ValueToEscape) + """"
ElseIf ValueToEscape.Contains("+") Then
)
Return (Convert.ToString("""") & ValueToEscape) + """"
Else
Return ValueToEscape
End If
End If
End Function