host-2008

导航

水晶报表(crystal report)添加参数及连接字符串

     在aspx 页面中,通过设置connectionstring 和 parameters 输入报表中,并通过 CrystalreportViewer 使用方法ExportToHttpResponse 输出为PDF 文件 供下载。

 private void ExportPdfReport()

{

            if (File.Exists(MapPath("~/Reports/DropshipInvoice.rpt")))
            {


                ReportDocument rptDropshipInvoice = new ReportDocument();

               //获取报表位置,并装载
                rptDropshipInvoice.Load(MapPath("~/Reports/DropshipInvoice.rpt"));
                rptDropshipInvoice.ReportOptions.EnableSaveDataWithReport = false;

                // initialize database connection information.设置连接字符串
                var connectionString =
                    new SqlConnectionStringBuilder(DbUtils.ActualConnectionString);

               //为 报表中的每个报表 或者子报表设置连接字符串
                foreach (IConnectionInfo connection in rptDropshipInvoice.DataSourceConnections)
                {
                    if (connectionString.IntegratedSecurity)
                    {
                        connection.SetConnection(connectionString.DataSource, connectionString.InitialCatalog,
                                                 connectionString.IntegratedSecurity);
                    }
                    else
                    {
                        connection.SetConnection(connectionString.DataSource, connectionString.InitialCatalog,
                                                 connectionString.UserID, connectionString.Password);
                    }
                }

                 //输入参数到报表中

                rptDropshipInvoice.SetParameterValue("beginDate", calandarShipDay.SelectedDate);
                rptDropshipInvoice.SetParameterValue("endDate", calandarShipDay.SelectedDate.AddDays(1));
                rptDropshipInvoice.SetParameterValue("CustomerUserID", CurrentLoginUser.UserID.ToString());
                rptDropshipInvoice.SetParameterValue("MainsiteApplicationID", SystemConfiguration.MainSiteApplicationId.ToString());

                 //导出报表

                ExportReport(rptDropshipInvoice, "DropshipInvoice" + calandarShipDay.SelectedDate.ToString("yyyyMMdd"), ExportFormatType.PortableDocFormat);
            }

//ExportReport 方法,在aspx页面中 导出report 模板中的数据,并生成一个pdf 文件

Private void ExportReport(对应参数)

{

 HttpContext.Current.Response.Buffer = false;
        
            // Clear the response content and headers
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();

            rptDropshipInvoice.ExportToHttpResponse(formatType, HttpContext.Current.Response, true, strExportFileName);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

}

posted on 2011-04-09 10:54  夜来风雨香  阅读(918)  评论(0编辑  收藏  举报