5 Dec 2006
Exporting Report in different formates
Private Sub ExportReport()
Dim str_L_Sql As String
Dim dr As SqlDataReader
Dim cmd As New SqlCommand
Dim conn As New SqlConnection(strCon)
Dim exportOpts As New ExportOptions()
Dim diskOpts As New DiskFileDestinationOptions()
Dim strFileName As String
Dim strFilePath As String
lblExport.Text = ""
Try
Dim path As String = Server.MapPath(CStr(Session("RepPath") & str_P_ReportName)) 'to get server mapped path.File should be under root directory
crReportDocument.Load(path) 'to load report from given path
exportOpts = crReportDocument.ExportOptions 'creates a object to export the report
'Now we have to login database.Server Name,Database name,user id and password is taken in session variables.you can write them directly
With crconnectioninfo
.ServerName = Session("SName")
.DatabaseName = Session("DBName")
.UserID = Session("Uid")
.Password = Session("Pwd")
End With
crtables = crReportDocument.Database.Tables
'Now login for each and every table and stored procedure used in crystal report
For Each crtable In crtables
crtablelogoninfo = crtable.LogOnInfo
crtablelogoninfo.ConnectionInfo = crconnectioninfo
crtable.ApplyLogOnInfo(crtablelogoninfo)
Next
'Parameters name,ordering of parameter and their default value is in a table to make everything dyanamic.you can write them static.Same code has to write repeatedly to add all parameters used in crystal report
str_L_Sql = "Select param_Name,orderNo from tbl_Report_D where report_ID=(Select report_ID from tbl_Report_M where report_Name='" & str_P_ReportName & "') order by orderNo"
conn.Open()
With cmd
.CommandType = CommandType.Text
.CommandText = str_L_Sql
.Connection = conn
End With
dr = cmd.ExecuteReader()
While dr.Read()
If CStr(Session(dr.Item(0).ToString)) <> "" Then
crParameterFields = crReportDocument.ParameterFields
crParameterField = crParameterFields.Item(CInt(dr.Item (1).ToString) - 1)
crParameterValues = crParameterField.CurrentValues
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = Session(dr.Item(0).ToString)
crParameterValues.Add(crParameterDiscreteValue)
End If
End While
strFilePath = ""
strFileName = ""
Select Case cboExportType.SelectedItem.Text
Case "Rich Text (RTF)"
exportOpts.ExportFormatType = ExportFormatType.RichText
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
strFilePath = Session("RepPath") & "Downloads\"
strFileName = "Report.rtf"
diskOpts.DiskFileName = Server.MapPath(Session("RepPath") & "Downloads\Report.rtf")
exportOpts.DestinationOptions = diskOpts
Case "Portable Document (PDF)"
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
strFilePath = Session("RepPath") & "Downloads\"
strFileName = "Report.pdf"
diskOpts.DiskFileName = Server.MapPath(Session("RepPath") & "Downloads\Report.pdf")
exportOpts.DestinationOptions = diskOpts
Case "MS Word (DOC)"
exportOpts.ExportFormatType = ExportFormatType.WordForWindows
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
strFilePath = Session("RepPath") & "Downloads\"
strFileName = "Report.doc"
diskOpts.DiskFileName = Server.MapPath(Session("RepPath") & "Downloads\Report.doc")
exportOpts.DestinationOptions = diskOpts
Case "MS Excel (XLS)"
exportOpts.ExportFormatType = ExportFormatType.Excel
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
strFilePath = Session("RepPath") & "Downloads\"
strFileName = "Report.xls"
diskOpts.DiskFileName = Server.MapPath(Session("RepPath") & "Downloads\Report.xls")
exportOpts.DestinationOptions = diskOpts
End Select
crReportDocument.Export()
lblExport.Visible = True
lblExport.Text = "Exported to the Destination Successfully!"
Catch err As Exception
lblExport.Visible = True
lblExport.Text = err.Message & "Error in Exporting!"
Finally
conn.Close()
cmd = Nothing
End Try
End Sub
Subscribe to:
Post Comments (Atom)
1 comment:
that very good
Post a Comment