473,403 Members | 2,071 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,403 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


Nov 17 '05 #1
0 1024

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...
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...
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. ...
3
by: Regnab | last post by:
Despite having at least 2 days VBA programming experience, this one has got me... I'm trying to export multiple tables into one worksheet in Excel. I've adapted code that I've got from this forum,...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.