<%@ Import Namespace ="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<script language="vb" runat ="server">
sub Page_Load()
Dim strconnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
strconnection += "Data Source = Teens.mdb"
Dim strSQL As String = "SELECT * FROM teens"
Dim objdataset As New DataSet
Dim objconnection As New OleDbConnection(strconnection)
Dim objadapter As New OleDbDataAdapter(strSQL, objconnection)
objadapter.Fill(objdataset, "teens")
Dim objdataview As New DataView(objdataset.Tables("teens"))
nwdat.DataSource = objdataview
nwdat.databind()
end sub
</script>
<html>
<body>
<h2>Teens Return</h2>
<asp:datagrid id="nwdat" runat = "server"/>
<br />
</body>
</html>
exporting to csv instead of datagrid
Page 1 of 11 Replies - 3104 Views - Last Post: 21 September 2011 - 06:57 AM
#1
exporting to csv instead of datagrid
Posted 08 September 2011 - 07:37 PM
I have a file that I can get to show correctly in grid/web - however I need to be able to download a CSV. I do not usually work in asp so out on a limb with a deadline here!
Replies To: exporting to csv instead of datagrid
#2
Re: exporting to csv instead of datagrid
Posted 21 September 2011 - 06:57 AM
Add a hyperlink to the page that the user can click to download the CSV file.
Create a new page called "ExportToCSV" or something that makes sense to you.
This page will have no HTML in it...The purpose of this page will be to take the data from the data table and convert it into Comma-Separated-Values. This page's content type will be changed to "Application/x-msexcel". This will inform the browser that the content being sent to it is not HTML.
For example, if your DataTable has already been populated and stored in session as "teensTable", you could have something like the following:
Please note that the above posted code is not 100% correct on purpose. It's just meant to give you a starting point.
Look up "CSV" or "Comma Separated Values" to learn about the rules about when you need to use double quotes etc.
Now that you have an aspx page that generates the csv file for you, all you have to do is call that page by setting it as the URL that the hyperlink opens when it's clicked.
-Frinny
Create a new page called "ExportToCSV" or something that makes sense to you.
This page will have no HTML in it...The purpose of this page will be to take the data from the data table and convert it into Comma-Separated-Values. This page's content type will be changed to "Application/x-msexcel". This will inform the browser that the content being sent to it is not HTML.
For example, if your DataTable has already been populated and stored in session as "teensTable", you could have something like the following:
Protected Sub Page_Load(ByVal sender As object, ByVal e As System.EventArgs) Handles Me.Load
'Clearing any HTML data already in the response stream and setting the content type to CSV'
Response.Clear()
Response.AddHeader("Content-Type", "Application/x-msexcel")
Response.AddHeader("Content-Disposition", "attachment;filename=teens.csv")
Dim teens As DataTable = TryCast(Session("teensTable"), DataTable)
If teens IsNot Nothing
Response.Write(GenearteCSV(teens))
Else
Response.Write("")
End If
End Sub
Public Function GenerateCSV(ByVal dt As DataTable) As String
Dim csv As New StringBuilder 'Will contain the csv'
Dim doubleQuote As String = """" 'Used to insert double quotes'
For Each row As DataRow In dt.Rows
For Each col As DataColumn In dt.Columns
Dim data = row.Item(col.ColumnName)
Dim str As String = data.ToString
'Please note that if the string is empty you should consider'
'adding an empty space surrounded by double quotes'
'Also, you need to replace any double quotes in the with 2 double'
'quotes so that it appears properly when you open the file'
If str.Contains(",") Then
str = doubleQuote + str + doubleQuote
End If
csv.Append(str)
csv.Append(",")
Next
'Removing the last "," from the row '
csv.Remove(csv.Length - 1, 1)
csv.Append(Environment.NewLine)
Next
Return csv.ToString
End Function
Please note that the above posted code is not 100% correct on purpose. It's just meant to give you a starting point.
Look up "CSV" or "Comma Separated Values" to learn about the rules about when you need to use double quotes etc.
Now that you have an aspx page that generates the csv file for you, all you have to do is call that page by setting it as the URL that the hyperlink opens when it's clicked.
-Frinny
This post has been edited by Frinavale: 21 September 2011 - 07:18 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|