【发布时间】:2015-06-11 18:41:07
【问题描述】:
我正在使用 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,0);
代码,其他人也建议在页面初始化中填充数据集,但我似乎不知道该怎么做。
这是我当前工作的代码(通常的打印过程,它将首先引导您进入 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, true, 0, 0);
con.Close();
}
}
更新:
我只是需要这个来更新以澄清以下接受的答案仅在服务器端有效。这意味着当用户远程访问服务器时,代码将不起作用。
【问题讨论】:
标签: c# asp.net crystal-reports