[轉貼] ASP.NET傳遞參數給Crystal Report做Export或Print的動作

2012030608:54

最近有網友提到此問題...小弟就做一個demo範例分享給大家呀..

首先準備一個crystal report檔(CrystalReport.rpt),內容如下:

欄位總管新增一個參數,如下所示:

接下來就是程式碼

asp.net(C#)

ReportView.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReportView.aspx.cs" Inherits="ReportView" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ReportView</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txbLogo" runat="server"></asp:TextBox>
            <asp:Button ID="btnPreview" runat="server" OnClick="btnPreview_Click" Text="Preview" />
            <asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export" />
            <asp:Button ID="btnPrint" runat="server" OnClick="btnPrint_Click" Text="Print" /><br />
            <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" DisplayToolbar="False" />
            <CR:CrystalReportSource ID="CrystalReportSource1" runat="server" />
        </div>
    </form>
</body>
</html>
ReportView.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ReportView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void btnPreview_Click(object sender, EventArgs e)
    {
        //load crystal report
        this.CrystalReportSource1.Report.FileName = Server.MapPath("CrystalReport.rpt");

        //load param
        CrystalDecisions.Web.Parameter param = new CrystalDecisions.Web.Parameter();
        param.Name = "logo";
        param.DefaultValue = this.txbLogo.Text;

        //add param
        this.CrystalReportSource1.Report.Parameters.Add(param);

        //bind
        this.CrystalReportViewer1.ReportSourceID = this.CrystalReportSource1.ID;
        this.CrystalReportViewer1.DataBind();

    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        //create CrystalReport object
        CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();

        //load report
        report.Load(Server.MapPath("CrystalReport.rpt"));

        //add param
        report.SetParameterValue("logo", this.txbLogo.Text);

        //print
        report.PrintToPrinter(1, false, 0, 0);
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        //create CrystalReport object
        CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();

        //load report
        report.Load(Server.MapPath("CrystalReport.rpt"));

        //add param
        report.SetParameterValue("logo", this.txbLogo.Text);

        //export
        report.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "test.pdf");
    }
}
 

執行結果:

參考網址:

http://www.dotblogs.com.tw/puma/archive/2008/07/16/4503.aspx