使用水晶报表制作的报表可以表格形式出现吗? [英] can reports made using crystal reports can be in form of table?

查看:92
本文介绍了使用水晶报表制作的报表可以表格形式出现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作桌面应用程序.
我已经使用Crystal创建了报表.我正在获取表中的数据.nw我希望数据应该以表格的形式显示,这是用excel或word制作销售报告的方式
预先感谢
问候

i am making desktop application.
i have created report using crystal .i am getting the data as it is in the table .nw i want that data should be displayed in form of table the way sales report are made in excel or word
thanx in advance
regards

推荐答案

此示例将需要什么:
Visual Studio 2005,Crystal Reports,SQL Server 2005
Crystal Reports将提示您提供用户名和密码,然后才能在应用程序中查看报告.要自动登录数据库并避免这种情况,可以通过编程方式进行.而不是将代码放置在Load事件中,而是将其放置在Init事件中.

这是绕过数据库登录提示的示例代码:
1)
What you will need for this example:
Visual Studio 2005, crystal reports, sql server 2005
Crystal Reports will prompt you to supply a username and password before you can see your report in your application. To automatically log in to the database and avoid this, you can do this programmatically. Instead of placing the code in the Load Event, place it in the Init event.

Here is the sample code to bypass the database login prompt:
1)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        ConfigureReport()
    End Sub


2)设置ConfigureReport()子例程:


2) Set up the ConfigureReport() subroutine:

Private Sub ConfigureReport()
       If Not Page.IsPostBack Then
           Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
           myConnectionInfo.DatabaseName = DatabaseName
           myConnectionInfo.UserID = userid
           myConnectionInfo.Password = password
           Dim ReportPath As String = Server.MapPath("YourReport.rpt")
           ReportViewer1.ReportSource = ReportPath
           SetDBLogonForReport(myConnectionInfo)
       End If


3)设置数据库登录子例程:


3) Set up the Database Logon subroutine:

Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo)
       Dim myTableLogOnInfos As TableLogOnInfos = ReportViewer1.LogOnInfo()
       For Each myTableLogOnInfo As TableLogOnInfo In myTableLogOnInfos
           myTableLogOnInfo.ConnectionInfo = myConnectionInfo
       Next
   End Sub


4)确保代码页顶部具有以下Import语句.


4) Make sure you have the following Import statements at the top of the code page.

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data
Imports System.Data.SqlClient


注意:最后一个import语句是您是否决定使用sql连接.
5)在sql server中创建您的参数化存储过程
示例存储过程:


Note: the last import statement is if you decide to use a sql connection.
5) Create your parameterized stored procedure in sql server
Sample stored procedure:

CREATE PROCEDURE sp_YourProcedure
@value1 int,
@value2 int
AS
BEGIN  
SET NOCOUNT ON;
SELECT * FROM Table WHERE column1=@value1 and column2=@value2
   
END


6)将此存储过程连接到Crystal报表(如果尚未使用其连接创建报表)

为此,请进入Visual Studio 2005,进入您的项目,然后单击网站>添加新项>选择水晶报表
为您的报告命名,然后继续执行向导.在数据库专家中,展开创建新连接,展开oledb(ado),选择SQL Server,输入服务器信息,选择数据库.点击下一步.展开您的服务器节点,展开您的数据库节点,展开dbo节点,展开存储过程节点,选择您的存储过程,然后通过单击>"将其放置在所选表"列中.按钮.单击确定.
在此过程中的某个地方,他们会要求您提供参数值的值,不提供任何值,只需单击确定"即可.
7)当报表在vs2005中出现时,进入字段资源管理器",展开数据库字段"节点,通过拖放将任何列添加到报表中.
8)返回您的代码页.将这些代码行添加到"ReportViewer1.ReportSource = ReportPath"行之后的ConfigureReport()子例程中.


6) Connect this stored procedure to your crystal report (if you haven''t created a report already with its connections)

To do this, go into Visual Studio 2005, go into your project, click website>add new item>choose crystal reports
Name your report and continue through the wizard. In Database Expert, expand create new connection, expand oledb (ado), select SQL Server, enter server info, select database. Click Next. Expand your server node, expand your database node, expand the dbo node, expand stored procedure node, select your stored procedure and place it in the Selected Tables column by clicking the ">" button. Click OK.
Somewhere in the process, they will ask for values for your parameter values, don''t provide any, just click OK.
7) When the report comes up in vs2005, go into Field Explorer, expand the Database Fields Node, add whatever columns to your report by drop and drag.
8) Go back into your code page. Add these lines of code to the ConfigureReport() subroutine after the line "ReportViewer1.ReportSource = ReportPath"

Dim field1 As ParameterField = Me.ReportViewer1.ParameterFieldInfo(0)
Dim field2 As ParameterField = Me.ReportViewer1.ParameterFieldInfo(1)
Dim val1 As New ParameterDiscreteValue()
Dim val2 As New ParameterDiscreteValue()
val1.Value = "value1"
val2.Value = "value2"
field1.CurrentValues.Add(val1)
field2.CurrentValues.Add(val2)


因为我有两个值,所以我需要两个离散值.无论您的参数值是什么值,它都将传递给value1和value2.这些值可以来自任何地方,包括用户文本框,组合框等.我自己使用了会话变量,但这有缺点.
这是完整的代码:


Because I have two values, I need two discrete values. Whatever the values of your parameter values, it will be passed to value1 and value2. These values can come from anywhere, a user textbox, combo box, etc. I have used session variables myself, but that has drawbacks.
Here is the complete code:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data
Imports System.Data.SqlClient
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        ConfigureReport()
    End Sub
Private Sub ConfigureReport()
        If Not Page.IsPostBack Then
            Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
            myConnectionInfo.DatabaseName = DatabaseName
            myConnectionInfo.UserID = userid
            myConnectionInfo.Password = password
            Dim ReportPath As String = Server.MapPath("YourReport.rpt")
            ReportViewer1.ReportSource = ReportPath
            Dim field1 As ParameterField = Me.ReportViewer1.ParameterFieldInfo(0)
            Dim field2 As ParameterField = Me.ReportViewer1.ParameterFieldInfo(1)
            Dim val1 As New ParameterDiscreteValue()
            Dim val2 As New ParameterDiscreteValue()
            val1.Value = "value1"
            val2.Value = "value2"
            field1.CurrentValues.Add(val1)
            field2.CurrentValues.Add(val2)

            SetDBLogonForReport(myConnectionInfo)
        End If
End Sub
Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo)
        Dim myTableLogOnInfos As TableLogOnInfos = ReportViewer1.LogOnInfo()
        For Each myTableLogOnInfo As TableLogOnInfo In myTableLogOnInfos
            myTableLogOnInfo.ConnectionInfo = myConnectionInfo
        Next
    End Sub


运行您的报告,它应该显示而不提示用户名,密码或参数值.
我希望这可以帮助像我一样试图获得有关此信息的沮丧之情的其他人.如果愿意,请随时对此解决方案发表评论.


Run your report and it should show up without prompting for a username, password or parameter values.
I hope this helps someone else who was just as frustrated as I was trying to get info on this. Please feel free to comment on this solution if you like.


这篇关于使用水晶报表制作的报表可以表格形式出现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆