水晶報表(crystal report)添加參數及連接字串

來源:互聯網
上載者:User

     在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();

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.