Visual Studio .NET 2002SQL Server 2000.NET 1.0DBAVisual Studio .NET 2003WebForms.NET 1.1中级开发Visual StudioSQL ServerSQLWindows.NETVisual BasicASP.NET
在 ASP.NET 中显示、导出和打印 Crystal Reports






3.36/5 (43投票s)
在 ASP.NET 中显示、导出和打印 Crystal Reports,附带源代码。
引言
本文档的主要目的是在不出现任何错误的情况下显示报表。我被“登录失败错误”困扰了好几天,现在我终于有了一个可以无错误显示报表的代码。该代码还可以将报表导出为 .pdf、.xls、.rtf 和 .doc 格式。它还可以直接将报表打印到打印机。
使用代码
解压缩 crCodes.Zip,然后运行 crCode.vbproj 项目文件。
--- 或者 ----
将 Webform1.aspx 文件插入到您的现有项目中,然后将 crystalreport2.rpt 文件复制到您的项目中,然后开始使用该代码。
Webform1.aspx.vb 的完整源代码如下。只需像图像中所示那样设计表单,放置 Crystal Report Viewer 控件,并保留控件的默认名称
Imports CrystalDecisions.Shared
Imports System.IO
Public Class WebForm1
Inherits System.Web.UI.Page
Dim crReportDocument As CrystalReport2 = New CrystalReport2
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents CrystalReportViewer1 As _
CrystalDecisions.Web.CrystalReportViewer
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required
'by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
' this is the most IMPORTANT line of code
' if this line is not writen the
' " LOGON FAILED" error starts displaying
crReportDocument.SetDatabaseLogon("username",_
"password", "sql-server", "database")
' the Above line works even if only username
' and password is supplied as below
'crReportDocument.SetDatabaseLogon("username",_
"password") ', "sql-server", "database")
' this will hide the group tree
CrystalReportViewer1.DisplayGroupTree = False
CrystalReportViewer1.ReportSource = crReportDocument
' IF REPORT Uses Parameter's
' Pass Paramaters As Follows
crReportDocument.SetParameterValue("city", "Mumbai")
' city = Parameter Name
' Mumbai = Parameter Value
' :-) thats ALL your Report Will Be displayed
' now Without Logon Failed Error
With DropDownList1.Items
.Add("Rich Text (RTF)")
.Add("Portable Document (PDF)")
.Add("MS Word (DOC)")
.Add("MS Excel (XLS)")
End With
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub ExportReport()
Dim oStream As New MemoryStream ' // using System.IO
'this contains the value of the selected export format.
Select Case DropDownList1.SelectedItem.Text
Case "Rich Text (RTF)"
'-----------------------------------------------------------
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.WordForWindows)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/rtf"
'------------------------------------------------------------
'------------------------------------------------------------
Case "Portable Document (PDF)"
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
'--------------------------------------------------------------
'--------------------------------------------------------------
Case "MS Word (DOC)"
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.WordForWindows)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/doc"
'---------------------------------------------------------------
'---------------------------------------------------------------
Case "MS Excel (XLS)"
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.Excel)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
'---------------------------------------------------------------
End Select 'export format
Try
Response.BinaryWrite(oStream.ToArray())
Response.End()
Catch err As Exception
Response.Write("< BR >")
Response.Write(err.Message.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
ExportReport()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
crReportDocument.SetDatabaseLogon("USER", _
"PASSWORD", "SQL-SERVER", "DATABASE")
crReportDocument.PrintToPrinter(1, False, 0, 0)
End Sub
End Class
为什么会生成“登录失败错误”?
“登录失败”错误基本上是由于在显示报表时,它会尝试登录到数据库服务器。即使您在设计时选择了服务器,报表在显示或导出或打印时仍然需要服务器名称。
消除此错误的编码行是
crReportDocument.SetDatabaseLogon("USER", "PASSWORD", "SQL-SERVER", "DATABASE")
或者也可以使用
crReportDocument.SetDatabaseLogon("USER", "PASSWORD")