加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

在ASP.NET C#中直接打印Crystal报表

发布时间:2020-12-16 07:06:26 所属栏目:asp.Net 来源:网络整理
导读:我使用SQL Server存储过程在Crystal Report中填充数据.我能够使用参数传递实现我想要的打印输出,并使用CR中的公式格式化每列. 在打印报告中,通常的过程是它将在Crystal Report Viewer中预览创建/生成的输出,然后,有打印,导出选项,它将首先将报告转换为PDF以
我使用SQL Server存储过程在Crystal Report中填充数据.我能够使用参数传递实现我想要的打印输出,并使用CR中的公式格式化每列.

在打印报告中,通常的过程是它将在Crystal Report Viewer中预览创建/生成的输出,然后,有打印,导出选项,它将首先将报告转换为PDF以继续打印功能

我想要的是当我点击打印按钮时,它会自动导致打印程序.

我找到了答案How to print crystal report directly to a client machine using C# Asp.net的链接

using oReportDocument.PrintToPrinter(1,true,0);

代码,其他人也建议填写页面init中的数据集,但我似乎迷失了如何做到这一点.

这是我目前正在使用的代码(通常的打印过程,它会引导您首先进入Crystal Report预览.

public partial class Report_MonthlySalesReportPrint : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    ReportDocument report = new ReportDocument();

    protected void Page_Load(object sender,EventArgs e)
    {
        con.Open();

        //Parameters to be passed as needed in formulas in Report
        string RP = Session["RP"].ToString();
        DateTime cms = Convert.ToDateTime(Session["CurrentMonthStart"].ToString());
        string Loc = Session["Location"].ToString();
        string MonthCurrent = Session["MonthCurrent"].ToString();
        string YearCurrent = Session["YearCurrent"].ToString();
        string MonthPrevious = Session["MonthPrevious"].ToString();
        string YearPrevious = Session["YearPrevious"].ToString();
        string MonthLastYear = Session["MonthLastYear"].ToString();
        string YearLastYear = Session["YearLastYear"].ToString();

        report.Load(Server.MapPath("MSRC.rpt"));

        CrystalReportViewer1.ReportSource = report;
        CrystalReportViewer1.ReuseParameterValuesOnRefresh = true;
        CrystalReportViewer1.DataBind();

        report.SetParameterValue(0,MonthLastYear);
        report.SetParameterValue(1,MonthCurrent);
        report.SetParameterValue(2,MonthPrevious);
        report.SetParameterValue(3,RP);
        report.SetParameterValue(4,Loc);
        report.SetParameterValue(5,cms);
        report.SetParameterValue(6,YearCurrent);
        report.SetParameterValue(7,YearPrevious);
        report.SetParameterValue(8,YearLastYear);

        report.PrintToPrinter(1,0);
        con.Close();
    }
}

更新:

我只是需要更新以便澄清,以下接受的答案仅适用于服务器端.用户远程访问服务器时的含义,代码将无法正常工作.

解决方法

using System.Drawing.Printing;
       using Crystal Decisions.CrystalReports.Engine;
       using Crystal Decisions.Shared;
       protected void Page_Load(object sender,EventArgs e)
       {
         void();
       }
       public void()
       {
         try
        {
         ReportDocument crystalReport = new ReportDocument();
         crystalReport.Load(Server.MapPath("~/CrystalReport2.rpt"));
         DataSet dsCustomers = GetData("select * from visitor_details where  id ='" + Session["sessionvid"] + "' and  plant ='" + Session["sessionplant"] + "'");

         DataTable dataTable = dsCustomers.Tables[0];   crystalReport.Database.Tables["visitor_details"].SetDataSource((DataTable)dataTable);
          CrystalReportViewer2.ReportSource = crystalReport;
          CrystalReportViewer2.Zoom(100);
          //crystalReportViewer1.ExportReport() ;
          CrystalReportViewer2.RefreshReport();
          crystalReport.PrintOptions.PrinterName = GetDefaultPrinter();
          crystalReport.PrintToPrinter(1,false,0);
        }
        catch
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('connect printer settings')</script>");
        }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读