Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Dim ExOPS As ExportOptions
Dim ReportDoc As ReportDocument = New ReportDocument
Dim logOnInfo As ReportInfo = New ReportInfo
Dim FileOPS As DiskFileDestinationOptions = New DiskFileDestinationOptions
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet1 = New DataSet1
ds.temptable.AddtemptableRow("熊川湘", 12)
ds.temptable.AddtemptableRow("唐沙", 24)
Dim cry As CrystalReport1 = New CrystalReport1
'程序外控制报表的标题
Dim to1 As CrystalDecisions.CrystalReports.Engine.TextObject
to1 = CType(cry.ReportDefinition.ReportObjects("text3"), CrystalDecisions.CrystalReports.Engine.TextObject)
to1.Text = "tttt"
cry.SetDataSource(ds.Tables("temptable"))
'cry.ExportToDisk(ExportFormatType.PortableDocFormat, "d:\\c.pdf") ''转换成PDF
'cry.ExportToDisk(ExportFormatType.WordForWindows, "d:\\c.doc")''转换成doc
cry.ExportToDisk(ExportFormatType.Excel, "d:\\c.xls") ''转换成xls
CrystalReportViewer1.ReportSource = cry
table1 = ds.Tables("temptable")
End Sub
Dim table1 As DataTable = New DataTable
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' ExportToPDF("d:\\CrystalReport1.rpt", Me.table1, "d:\\c.doc")
ExportToExcel("d:\\CrystalReport1.rpt", Me.table1, "d:\\c.xls")
End Sub
''' <summary>
''' 导出报表文件为PDF格式
''' </summary>
''' <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
''' <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
''' <param name="PDFFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
''' <returns>bool成功返回true,失败返回false</returns>
Public Function ExportToPDF(ByVal ReportFile As String, ByVal ReportDataSource As Object, ByVal PDFFileName As String) As Boolean
Try
ReportDoc.Load(ReportFile)
ReportDoc.SetDataSource(ReportDataSource)
FileOPS.DiskFileName = PDFFileName
ExOPS = ReportDoc.ExportOptions
ExOPS.DestinationOptions = FileOPS
ExOPS.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
ExOPS.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
ReportDoc.Export()
Return True
Catch
Return False
End Try
End Function
''' <summary>
''' 导出报表文件到Excel格式
''' </summary>
''' <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
''' <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
''' <param name="ExcelFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
''' <returns>成功返回true失败返回false</returns>
Public Function ExportToExcel(ByVal ReportFile As String, ByVal ReportDataSource As Object, ByVal ExcelFileName As String) As Boolean
Try
ReportDoc.Load(ReportFile)
ReportDoc.SetDataSource(ReportDataSource)
FileOPS.DiskFileName = ExcelFileName
ExOPS = ReportDoc.ExportOptions
ExOPS.DestinationOptions = FileOPS
ExOPS.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
ExOPS.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.Excel
ReportDoc.Export()
Return True
Catch
Return False
End Try
End Function
End Class