在 c# winform 中打印和制作报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14307452/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:23:31  来源:igfitidea点击:

Printing and making reports in c# winform

c#winformsprintingreport

提问by Andres

I used in Delphi QuickReport to create reports and print. What can I use to do this in .NET C#?

我在 Delphi QuickReport 中使用来创建报告和打印。我可以用什么来在 .NET C# 中做到这一点?

I added some reporting elements (Microsoft reports and Crystal reports) to my project (Winforms app), but what I saw, is that I can only insert data from a Database. What I want, is to use the values of objects created in runtime. This is because my reports actually consists of receipts and invoices.

我在我的项目(Winforms 应用程序)中添加了一些报告元素(Microsoft 报告和 Crystal 报告),但我看到的是,我只能从数据库中插入数据。我想要的是使用在运行时创建的对象的值。这是因为我的报告实际上包括收据和发票。

Which is the best tool to use for my need?

哪个是最适合我需要的工具?

采纳答案by John Koerner

You can use the built in reports to generate nice reports wihtout requiring a database.

您可以使用内置报告生成漂亮的报告,而无需数据库。

Create a class for your data, in my case, I am going to create a person class:

为您的数据创建一个类,就我而言,我将创建一个 person 类:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
}
  • Next I am going to add a report using the report wizard (Add New Item -> Reporting -> Report Wizard).

  • For the datasource, I am going to select Object and then point it to my Person class. Datasource from a class

  • Select the columns you want for your details, I am just dragging all of them into the values for simplicity.

  • 接下来,我将使用报告向导(添加新项目 -> 报告 -> 报告向导)添加报告。

  • 对于数据源,我将选择 Object,然后将其指向我的 Person 类。 来自类的数据源

  • 选择您想要的列作为详细信息,为了简单起见,我只是将它们全部拖到值中。

Report Wizard Fields

报告向导字段

  • Walk through the rest of the wizard just selecting the defaults and you should then see your report.

  • Now you can add a ReportViewer control to a form and set the report to the report you just created. This should create a PersonBindingSource on your form as well.

  • Set the PersonBindingSource's data to a list in memory:

    BindingList<Person> myPeople = new BindingList<Person>();
    myPeople.Add(new Person() { FirstName = "John" , LastName = "Doe"});
    myPeople.Add(new Person() { FirstName = "Jane" , LastName = "Doe"});
    myPeople.Add(new Person() { FirstName = "Jerry" , LastName = "Smithers" });
    
    PersonBindingSource.DataSource = myPeople;
    reportViewer1.RefreshReport();
    this.reportViewer1.RefreshReport();
    
  • 完成向导的其余部分,只需选择默认值,然后您应该会看到您的报告。

  • 现在,您可以将 ReportViewer 控件添加到表单并将报告设置为您刚刚创建的报告。这也应该在您的表单上创建一个 PersonBindingSource。

  • 将 PersonBindingSource 的数据设置为内存中的列表:

    BindingList<Person> myPeople = new BindingList<Person>();
    myPeople.Add(new Person() { FirstName = "John" , LastName = "Doe"});
    myPeople.Add(new Person() { FirstName = "Jane" , LastName = "Doe"});
    myPeople.Add(new Person() { FirstName = "Jerry" , LastName = "Smithers" });
    
    PersonBindingSource.DataSource = myPeople;
    reportViewer1.RefreshReport();
    this.reportViewer1.RefreshReport();
    

With the final report looking like this:

最终报告如下所示:

Final Report

总结报告

回答by FrostyFire

Crystal Reports will work perfectly for you. Actually you can generate reports without a database. Check out thisproject and it should get you started and is just like what you are trying to do.

Crystal Reports 非常适合您。实际上,您可以在没有数据库的情况下生成报告。看看这个项目,它应该让你开始,就像你想要做的一样。

You this helps you!

你这对你有帮助!