473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Data Export / Download Problem

I have the following class that I've wirtten to take a Dataset and
automatically export it to either XML, ASCII or Tab delimited file. The
reason I wrote it they way I did was that I don't want to have o create a
temporary file on the web server everytime someone want to run and export.
When I call the ExportToBrowser function the user gets the standard
open/save file dialog box. If the user chooses to save the file the program
works fine, but if they choose to open the file they get to an error the the
file does not exist in the tempory internet file directory. How can I get
this code to work as if the user had clicked on a link directly to a file on
the web server? Is there a command that I can use to write the file to the
temporary internet file so that if the user choses to open the file they
program works? Any help will be greatly appreciated.
Below is the code that is in the class I wrote to accomplish the task above.

Imports System.Text

Public Class DBExport
Private _DataSet As New DataSet
Private _IncludeHeader As Boolean = True
Private _IncludeXMLSchema As Boolean = False
Private _ExportType As Export_Type
Private _FileName As String

Public Enum Export_Type
XML = 0
ASCII = 1
TAB = 2
End Enum

Public Property DataSet() As DataSet
Get
Return _DataSet
End Get
Set(ByVal Value As DataSet)
_DataSet = Value
End Set
End Property

Public Property IncludeHeader() As Boolean
Get
Return _IncludeHeader
End Get
Set(ByVal Value As Boolean)
_IncludeHeader = Value
End Set
End Property

Public Property IncludeXMLSchema() As Boolean
Get
Return _IncludeXMLSchema
End Get
Set(ByVal Value As Boolean)
_IncludeXMLSchema = Value
End Set
End Property

Public Property ExportType() As Export_Type
Get
Return _ExportType
End Get
Set(ByVal Value As Export_Type)
_ExportType = Value
End Set
End Property

Public Property FileName() As String
Get
Return _FileName
End Get
Set(ByVal Value As String)
_FileName = Value
End Set
End Property

Public Function ExportToBrowser(ByVal CurrentResponse As
System.Web.HttpResponse) As String
Select Case _ExportType
Case Is = Export_Type.XML
'Setup response to export data
CurrentResponse.AppendHeader("content-disposition", "attachment;
filename=" & _FileName & ".xml")
CurrentResponse.ContentType = "Text/XML"

'Write schema to export
If _IncludeXMLSchema Then
CurrentResponse.Write(_DataSet.GetXmlSchema)
End If

'Write data to export
CurrentResponse.Write(_DataSet.GetXml())
Case Is = Export_Type.ASCII
'Setup response to export data
CurrentResponse.AppendHeader("content-disposition", "attachment;
filename=" & _FileName & ".txt")

If _IncludeHeader Then
CurrentResponse.Write(GetHeader())
End If

CurrentResponse.Write(GetData())
Case Is = Export_Type.TAB
'Setup response to export data
CurrentResponse.AppendHeader("content-disposition", "attachment;
filename=" & _FileName & ".xls")

If _IncludeHeader Then
CurrentResponse.Write(GetHeader())
End If

CurrentResponse.Write(GetData())
End Select

CurrentResponse.End()
End Function

Private Function GetHeader() As String
Dim iXcounter As Integer
Dim sbHeader As New StringBuilder
Dim stDelimiter As String

If _ExportType = Export_Type.ASCII Then
stDelimiter = ", "
Else
stDelimiter = Chr(9)
End If

For iXcounter = 0 To _DataSet.Tables(0).Columns.Count - 1

sbHeader.Append(_DataSet.Tables(0).Columns(iXcount er).ColumnName)

If iXcounter <> _DataSet.Tables(0).Columns.Count - 1 Then
sbHeader.Append(stDelimiter)
Else
sbHeader.Append(Chr(13))
End If
Next

'If Tab delimited add extra line feed so there is a blank line
between the header and first
'record of data
If _ExportType = Export_Type.TAB Then
sbHeader.Append(Chr(13))
End If

Return sbHeader.ToString()
End Function

Private Function GetData() As String
Dim iXcounter As Integer
Dim iYcounter As Integer
Dim sbData As New StringBuilder
Dim stDelimiter As String

If _ExportType = Export_Type.ASCII Then
stDelimiter = ", "
Else
stDelimiter = Chr(9)
End If

For iXcounter = 0 To _DataSet.Tables(0).Rows.Count - 1
For iYcounter = 0 To
_DataSet.Tables(0).Rows(iXcounter).ItemArray.GetUp perBound(0)

sbData.Append(_DataSet.Tables(0).Rows(iXcounter).I temArray.GetValue(iYcounte
r))
If iYcounter <> _DataSet.Tables(0).Columns.Count - 1 Then
sbData.Append(stDelimiter)
Else
sbData.Append(Chr(13))
End If
Next
Next

Return sbData.ToString()
End Function
End Class

Thanks,

Shawn Mehaffie
PC Resources, LLC
Jul 21 '05 #1
0 1868

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
1
by: bigoxygen | last post by:
Hi. I would like to offer the users of my web application the ability to export data in the csv format. What I have come up with currently is writing the stream of data into a csv file which...
14
by: atse | last post by:
Hi experts, I retrieve data from the database and display on ASP, then I export these data to a file, like Excel (the best) or text file. Is it possible? I think it is possible, but how can I do...
7
by: Norm | last post by:
Hi All, I have an MDB file which I want to remain secure. It checks for certain parameters upon startup, and will automatically exit if the program is opened/executed by an unauthorized user. ...
0
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to...
1
by: warlord | last post by:
In order to save typing, I've borrowed the text from a post of nearly 12 months ago.....but the problem still exists. I've been banging my head all day with this, so I'm hoping someone has some...
2
by: =?Utf-8?B?Tmc=?= | last post by:
Hi. have someone can give me some idea how to start. the thing i want do is using web page export the sql data to text file then let user download. eg. the webpage has one button. when user...
2
by: farukcse | last post by:
Dear Sir, I have a PHP script that export CSV with arabic text, for example I download the CSV on the website then save to desktop of my computer and Open it on NOTEPAD... that works fine I could...
1
by: bienwell | last post by:
Hi all, I'm going to develop an ASP.net page (VB) that allows users to export data into the zip file. Do you have any sample codes? Please give me the source codes if you have. One more...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.