如何在 C# 中不显示 PrintDialog() 直接打印 rdlc 报告?

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

How to directly print rdlc report without showing PrintDialog() in C#?

c#printingrdlc

提问by Redone

I have an application where I have to print a RDLCreport without showing the printDialog and using the default specified printer defined in the application. Below is my test implementaion code.

我有一个应用程序,我必须在RDLC不显示 printDialog 并使用应用程序中定义的默认指定打印机的情况下打印报告。下面是我的测试实现代码。

    Microsoft.Reporting.WinForms.ReportViewer reportViewerSales = new    Microsoft.Reporting.WinForms.ReportViewer();
    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceSales = new Microsoft.Reporting.WinForms.ReportDataSource();

    reportViewerSales.Reset();
        reportViewerSales.LocalReport.ReportPath = @"Sales.rdlc";

        reportDataSourceSales.Name = "SalesTableDataSet";

        int i = 1;
        foreach (Product item in ProductSalesList)
        {
            dataset.CurrentSales.AddCurrentSalesRow(i, item.Name, item.Quantity.ToString(), item.Price.ToString(), item.Price.ToString());
            i++;
        }
        reportDataSourceSales.Value = dataset.CurrentSales;
        reportViewerSales.LocalReport.DataSources.Add(reportDataSourceSales);
        dataset.EndInit();

        reportViewerSales.RefreshReport();
        reportViewerSales.RenderingComplete += new RenderingCompleteEventHandler(PrintSales);

And here is my Rendering Complete Method

这是我的渲染完成方法

public void PrintSales(object sender, RenderingCompleteEventArgs e)
    {
        try
        {

            reportViewerSales.PrintDialog();
            reportViewerSales.Clear();
            reportViewerSales.LocalReport.ReleaseSandboxAppDomain();
        }
        catch (Exception ex)
        {
        }
    }

采纳答案by tezzo

I just gave a quick look to a class I created to print directly and I think I took some ideas from this walkthrough: Printing a Local Report without Preview

我只是快速浏览了一个我创建的直接打印的类,我想我从这个演练中得到了一些想法: Printing a Local Report without Preview

回答by Sunil Motwani

public void PrintSales(object sender, RenderingCompleteEventArgs e)
{
    try
    {
        reportViewerSales.PageSetupDailog();
        reportViewerSales.PrintDialog();
        reportViewerSales.Clear();
        reportViewerSales.LocalReport.ReleaseSandboxAppDomain();
    }
    catch (Exception ex)
    {
    }
}

回答by shakee93

i have made an extension class to @tezzos answer. which might make it more easier.

我已经为@tezzos 答案制作了一个扩展课程。这可能使它更容易。

use this Gist Hereto get the extension class i wrote. include it to your project. don't for get namespace :D

使用这个Gist Here来获取我写的扩展类。将其包含到您的项目中。不要获取命名空间:D

LocalReport report = new LocalReport();
            report.ReportEmbeddedResource = "Your.Reports.Path.rdlc";
            report.DataSources.Add(new ReportDataSource("DataSet1", getYourDatasource()));
            report.PrintToPrinter();

PrintToPrinterMethod will be available on LocalReport. Might Help someone

PrintToPrinter方法将在LocalReport. 可能会帮助某人