S
Shawn Mehaffie
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(iXcounter).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.GetUpperBound(0)
sbData.Append(_DataSet.Tables(0).Rows(iXcounter).ItemArray.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
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(iXcounter).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.GetUpperBound(0)
sbData.Append(_DataSet.Tables(0).Rows(iXcounter).ItemArray.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