在 Visual Studio 2005 中使用 Crystal Reports(C# .NET Windows 应用程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/164621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Crystal Reports in Visual Studio 2005 (C# .NET Windows App)
提问by Piku
I need to create reports in a C# .NET Windows app. I've got an SQL Server 2005 database, Visual Studio 2005 and am quite OK with creating stored procedures and datasets.
我需要在 C# .NET Windows 应用程序中创建报告。我有一个 SQL Server 2005 数据库,Visual Studio 2005,并且非常擅长创建存储过程和数据集。
Can someone please point me in the right direction for creating reports? I just can't seem work it out. Some examples would be a good start, or a simple How-to tutorial... anything really that is a bit better explained than the MSDN docs.
有人可以指出我创建报告的正确方向吗?我似乎无法解决。一些示例将是一个好的开始,或者一个简单的操作教程......任何比 MSDN 文档更好解释的东西。
I'm using the CrystalDecisions.Windows.Forms.CrystalReportViewer control to display the reports, I presume this is correct.
我正在使用 CrystalDecisions.Windows.Forms.CrystalReportViewer 控件来显示报告,我认为这是正确的。
If I'm about to embark on a long and complex journey, what's the simplest way to create and display reports that can also be printed?
如果我即将踏上漫长而复杂的旅程,创建和显示也可以打印的报告的最简单方法是什么?
采纳答案by Piku
I have managed to make this work now.
我现在已经设法完成了这项工作。
Brief Overview
简要概述
It works by having a 'data class' which is just a regular C# class containing variables and no code. This is then instantiated and filled with data and then placed inside an ArrayList. The ArrayList is bound to the report viewer, along with the name of the report to load. In the report designer '.Net Objects' are used, rather than communicating with the database.
它的工作原理是拥有一个“数据类”,它只是一个包含变量而没有代码的常规 C# 类。然后将其实例化并填充数据,然后放置在 ArrayList 中。ArrayList 与要加载的报告的名称一起绑定到报告查看器。在报表设计器中使用“.Net Objects”,而不是与数据库通信。
Explanation
解释
I created a class to hold the data for my report. This class is manually filled by me by manually retrieving data from the database. How you do this doesn't matter, but here's an example:
我创建了一个类来保存我的报告的数据。这个类是我通过手动从数据库中检索数据来手动填充的。你如何做到这一点并不重要,但这里有一个例子:
DataSet ds = GeneratePickingNoteDataSet(id);
foreach (DataRow row in ds.Tables[0].Rows) {
CPickingNoteData pickingNoteData = new CPickingNoteData();
pickingNoteData.delivery_date = (DateTime)row["delivery_date"];
pickingNoteData.cust_po = (int)row["CustomerPONumber"];
pickingNoteData.address = row["CustomerAddress"].ToString();
// ... and so on ...
rptData.Add(pickingNoteData);
}
The class is then put inside an ArrayList. Each element in the arraylist corresponds to one 'row' in the finished report.
然后将该类放在 ArrayList 中。arraylist 中的每个元素对应于完成报告中的一个“行”。
The first element in the list can also hold the report header data, and the last element in the list can hold the report footer data. And because this is an ArrayList, normal Array access can be used to get at them:
列表中的第一个元素还可以保存报表标题数据,列表中的最后一个元素可以保存报表页脚数据。因为这是一个 ArrayList,所以可以使用普通的 Array 访问来获取它们:
((CPickingNoteData)rptData[0]).header_date = DateTime.Now;
((CPickingNoteData)rptData[rptData.Count-1]).footer_serial = GenerateSerialNumber();
Once you have an arraylist full of data, bind it to your report viewer like this, where 'rptData' is of type 'ArrayList'
一旦您拥有一个充满数据的数组列表,就可以像这样将其绑定到您的报告查看器,其中“rptData”的类型为“ArrayList”
ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(reportPath);
reportDoc.SetDataSource(rptData);
crystalReportViewer.ReportSource = reportDoc;
Now you will need to bind your data class to the report itself. You do this inside the designer:
现在您需要将数据类绑定到报表本身。您在设计器中执行此操作:
- Open the Field Explorer tab (which might be under the 'View' menu), and right-click "Database Fields"
- Click on 'Project Data'
- Click on '.NET Objects'
- Scroll down the list to find your data class (if it isn't there, compile your application)
- Press '>>' and then OK
- You can now drag the class members onto the report and arrange them as you want.
- 打开字段资源管理器选项卡(可能位于“查看”菜单下),然后右键单击“数据库字段”
- 点击“项目数据”
- 单击“.NET 对象”
- 向下滚动列表以找到您的数据类(如果不存在,请编译您的应用程序)
- 按“>>”然后确定
- 您现在可以将班级成员拖到报告上并根据需要进行排列。
回答by alexmac
Crystal is one possible option for creating reports. It has been around a long time and a lot of people seem to like it.
Crystal 是创建报告的一种可能选择。它已经存在了很长时间,而且似乎很多人都喜欢它。
You might want to take a look at SQL reporting services. I have used both but my preferance is SQL reporting services. Its pretty well integrated into studio and works similar to the other microsoft projects. Its also free with the sql express etc.
您可能想看看 SQL 报告服务。我两个都用过,但我更喜欢 SQL 报告服务。它很好地集成到工作室中,并且与其他微软项目类似。它也可以免费使用 sql express 等。
This is a good article on beginning reporting services: http://www.simple-talk.com/sql/learn-sql-server/beginning-sql-server-2005-reporting-services-part-1/
这是一篇关于开始报告服务的好文章:http: //www.simple-talk.com/sql/learn-sql-server/beginning-sql-server-2005-reporting-services-part-1/
回答by Steven A. Lowe
I second alex's recommendation to look at sql reporting services - if you have a sql developer license, then you probably already have reporting services
我第二个亚历克斯的建议看 sql 报告服务 - 如果你有一个 sql 开发人员许可证,那么你可能已经拥有报告服务
i don't like crystal reports, too much tedium in the designer (editing expressions all the time) too many server-deployment issues (check those license files!)
我不喜欢水晶报告,设计器中的太多乏味(一直在编辑表达式)太多服务器部署问题(检查那些许可证文件!)
回答by SeaDrive
I use Crystal. I will outline my method briefly, but be aware that I'm a one man shop and it may not translate to your environment.
我用水晶。我将简要概述我的方法,但请注意,我是一个人的商店,它可能无法转化为您的环境。
First, create a form with a CR Viewer. Then:
首先,创建一个带有 CR 查看器的表单。然后:
1) Figure out what data you need, and create a view that retrieves the desired columns. 2) Create a new Crystal report using the wizard giving your view as the source of the data. 3) Drag, drop, insert, delete, and whatever to rough your report into shape. Yes, it's tedious. 4) Create the necessary button click or whatever, and create the function in which to generate the report. 5) Retrieve the data to a DataTable (probably in a DataSet). You do not have to use the view. 6) Create the report object. Set the DataTable to be the DataSource. Assign the report object to the CR Viewer. This is one part for which there are examples.
1) 弄清楚您需要什么数据,并创建一个检索所需列的视图。2) 使用向导创建一个新的 Crystal 报表,将您的视图作为数据源。3) 拖放、插入、删除以及任何使您的报告粗略成形的操作。是的,这很乏味。4) 创建必要的按钮单击或其他什么,并创建生成报告的函数。5) 将数据检索到 DataTable(可能在 DataSet 中)。您不必使用该视图。6) 创建报表对象。将 DataTable 设置为 DataSource。将报告对象分配给 CR 查看器。这是有例子的部分。
Comments:
注释:
If you lose the window with the database fields, etc (Field Explorer), go to View/Document Outline. (It's my fantasy to have Bill Gates on a stage and ask him to find it.)
如果您丢失了包含数据库字段等的窗口(字段资源管理器),请转到查看/文档大纲。(我的幻想是让比尔盖茨站在舞台上并让他找到它。)
The reason for setting up the view is that if you want to add a column, you revise the view, and the Field Explorer will update automatically. I've had all sorts of trouble doing it other ways. This method also is a work-around for a bug that requires scanning through all the tables resetting which table they point to. You want to hand Crystal a single table. You do not want to try to get Crystal to join tables, etc. I don't say it doesn't work; I say it's harder.
设置视图的原因是,如果要添加列,则修改视图,字段资源管理器会自动更新。我在用其他方式做这件事时遇到了各种各样的麻烦。此方法也是解决需要扫描所有表以重置它们指向的表的错误的解决方法。你想递给水晶一张桌子。您不想尝试让 Crystal 加入表等。我不是说它不起作用;我说更难。
There is (or was) documentation for the VS implementation of Crystal on the Business Objects web site, but I believe that it has disappeared behind a register/login screen. (I could stand more info on that myself.)
在 Business Objects 网站上有(或曾经)有关于 Crystal 的 VS 实现的文档,但我相信它已经消失在注册/登录屏幕后面。(我自己可以了解更多信息。)
I've had trouble getting Crystal to page break when I want, and not page break when I don't want, etc. It's far from the best report writer I've ever used and I do not understand why it seems to have put so many others out of business. In addition, their licensing policies are very difficult to deal with in a small, fluid organization.
我在需要时让 Crystal 进行分页时遇到了麻烦,而在我不想要时却无法分页,等等。它远不是我用过的最好的报告编写器,我不明白为什么它似乎已经把这么多其他人倒闭了。此外,他们的许可政策在一个小型、流动的组织中很难处理。
Edited to add example:
编辑添加示例:
AcctStatement oRpt = new AcctStatement() ;
oRpt.Database.Tables[0].SetDataSource(dsRpt.Tables[0]);
oRpt.SetParameterValue("plan_title",sPlanName) ;
crViewer.ReportSource = oRpt ;
回答by mattlant
You can use the report viewer with client side reporting built into vs.net (ReportBuilder/ReportViewer control). You can create reports the same way as you do for sql reporting services, except you dont need sql server(nor asp.net). Plus you have complete control over them(how you present, how you collect data, what layer they are generated in, what you do with them after generating, such as mailing them, sending to ftp, etc). You can also export as PDF and excel.
您可以使用带有内置于 vs.net 的客户端报告的报告查看器(ReportBuilder/ReportViewer 控件)。您可以像创建 sql 报告服务一样创建报告,只是您不需要 sql server(也不需要 asp.net)。此外,您可以完全控制它们(您如何呈现、如何收集数据、在哪个层生成它们、生成后如何处理它们,例如邮寄它们、发送到 ftp 等)。您还可以导出为 PDF 和 Excel。
And in your case building up a report from data and user input, this may work great as you can build up your own datasource and data as you go along. Once your data is ready to be reported on, bind it to your report.
在您根据数据和用户输入构建报告的情况下,这可能会非常有效,因为您可以在进行过程中构建自己的数据源和数据。准备好报告您的数据后,将其绑定到您的报告。
The reports can easily be built in Visual Studio 2005 (Add a report to your project), and be shown in a Winforms app using the ReportViewer control.
可以在 Visual Studio 2005 中轻松构建报告(向项目添加报告),并使用 ReportViewer 控件在 Winforms 应用程序中显示。
Here is a great book i recommend to everyone to look at if interested in client side reports. It gives a lot of great info and many different scenarios and ways to use client side reporting.
这是一本很棒的书,我推荐给对客户端报告感兴趣的每个人。它提供了大量重要信息以及使用客户端报告的许多不同场景和方法。
回答by Piku
I found the following websites solved my problems. Included here for future reference.
我发现以下网站解决了我的问题。包括在此处以供将来参考。
CrystalReportViewer Object Model Tutorialsfor the tutorial on how to make the whole thing work. And also Setting up a project to use Crystal Reportsand specifically preparing the formand adding the control
CrystalReportViewer 对象模型教程,提供有关如何使整个工作正常进行的教程。并且还设置了一个项目以使用 Crystal Reports并专门准备表单并添加控件
回答by Kramii
I strongly recommend trying an alternative reporting solution - I have a lot of experience with Crystal, and have managed to do some funky things with it in .Net, but quite honestly the integration of Crystal and .Net is an absolute pig for anything but the simplest cases.
我强烈建议尝试另一种报告解决方案 - 我在 Crystal 方面有很多经验,并且已经设法在 .Net 中用它做一些时髦的事情,但老实说,Crystal 和 .Net 的集成对于任何事情来说都是绝对的猪,除了最简单的情况。
回答by kumar
i think this may help you out http://infynet.wordpress.com/2010/10/06/crystal-report-in-c/
我认为这可以帮助你 http://infynet.wordpress.com/2010/10/06/crystal-report-in-c/
回答by Mark Vick
I have tried RS. I am converting from RS back to Crystal. RS is just too heavy and slow (or something). There is no reason to have to wait 30 seconds for a report to render is RS when Crystal does it in under a second.
我试过RS。我正在从 RS 转换回 Crystal。RS 太重太慢(或其他什么)。当 Crystal 在不到一秒钟的时间内完成报告时,没有理由必须等待 30 秒才能呈现 RS。