I've used this and it is working for me. I'm using
VB.NET, so I'm not certain how to make it fit your environment.
Sub ExportReport()
' This subroutine uses a case statement to determine the selected export format from the dropdownlist
' menu and then sets the appropriate export options for the selected export format. The report is
' exported to a subdirectory called "C:\ExportFile\".
' ********************************
'Check to see if the application directory has a subdirectory called "C:\ExportFile\".
'If not, create the directory since exported files will be placed here.
'This uses the Directory class of the System.IO namespace.
Dim ExportPath As String
ExportPath = "C:\ExportFile\"
If Directory.Exists(ExportPath) = False Then
Directory.CreateDirectory("C:\ExportFile")
End If
' ********************************
'Set the time for the file name
Dim DateTimeNow As DateTime
Dim Yr As Int16
Dim Yr2 As String
Dim Mth As Int16
Dim mth2 As String
Dim Dy As Int16
Dim dy2 As String
Dim Hr As Int16
Dim hr2 As String
Dim Mn As Int16
Dim mn2 As String
Dim strDateTimeNow As String
DateTimeNow = Now()
Yr = Year(DateTimeNow)
Yr2 = Yr.ToString
Mth = Month(DateTimeNow)
If Mth <= 9 Then
mth2 = "0" + Mth.ToString
Else
mth2 = Mth.ToString
End If
Dy = Day(DateTimeNow)
If Dy <= 9 Then
dy2 = "0" + Dy.ToString
Else
dy2 = Dy.ToString
End If
Hr = Hour(DateTimeNow)
If Hr <= 9 Then
hr2 = "0" + Hr.ToString
Else
hr2 = Hr.ToString
End If
Mn = Minute(DateTimeNow)
If Mn <= 9 Then
mn2 = "0" + Mn.ToString
Else
mn2 = Mn.ToString
End If
strDateTimeNow = Yr2 + mth2 + dy2 + hr2 + mn2
' ********************************
'IF you have logons, use this next section of code
' First we must create a new instance of the diskfiledestinationoptions class and
' set variable called crExportOptions to the exportoptions class of the reportdocument.
Dim crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables As Tables
Dim CrTable As Table
Dim TableCounter
Dim crReportDocument As New ReportDocument
'I had previously defined my file name to be exported in the variable
'ReportFullFileName
crReportDocument.Load(ReportFullFileName)
'Set the ConnectionInfo properties for logging on to the Database
'If you are using ODBC, this should be the DSN name NOT the physical server name. If
'you are NOT using ODBC, this should be the physical server name
With crConnectionInfo
.ServerName = "Server"
'If you are connecting to Oracle there is no DatabaseName. Use an empty string.
'For example, .DatabaseName = ""
.DatabaseName = ""
'I had previously captured the UserID and Password and passed them to
'variables strUID and strPWD.
.UserID = strUID
.Password = strPWD
End With
'This code works for both user tables and stored procedures. Set the CrTables to the Tables collection
'of the report
CrTables = crReportDocument.Database.Tables
'Loop through each table in the report and apply the LogonInfo information
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next
crDiskFileDestinationOptions = New DiskFileDestinationOptions
crExportOptions = crReportDocument.ExportOptions
'Though not used in this sample, there are options that can be specified for various format types.
'When exporting to Rich Text, Word, or PDF, you can use the PdfRtfWordFormatOptions class to specify the
'first page, last page or page range to be exported.
'When exporting to Excel, you can use the ExcelFormatOptions class to specify export properties such as
'the column width etc.
'Export to PDF
'append a filename to the export path and set this file as the filename property for
'the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + ReportName + strDateTimeNow + ".pdf"
'set the required report ExportOptions properties
With crExportOptions
.DestinationOptions = crDiskFileDestinationOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
End With
Try
' Export the report
crReportDocument.Export()
Catch err As Exception
Response.Write("<BR>")
Response.Write(err.Message.ToString)
End Try
End Sub