水晶報表匯出pdf功能(完整的代碼C#版)

來源:互聯網
上載者:User

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//引入水晶報表的兩個DLL
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using Ghy.Funds.FundsClassLibrary;
using System.Data.SqlClient;

namespace WebAppTest
{
 /// <summary>
 /// WebForm1 的摘要說明。
 /// </summary>
 public class WebForm2 : System.Web.UI.Page
 {
  
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
  protected System.Web.UI.WebControls.Button Btn_Export2;
  ReportDocument reportDoc= new ReportDocument();

  //頁面載入
  SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
  //SqlConnection myconn1 = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
  //讀取web.config設定檔中的資料庫連接字串,並串連到指定的資料庫
  
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
   //reportDataBind();
   if (!Page.IsPostBack)
   {
    //測試的時候用綁定 
    reportDataBind();
   }
   
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {   
   this.Btn_Export2.Click += new System.EventHandler(this.Btn_Export2_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  //綁定資料來源
  private void reportDataBind()
  {
     string sql = "SELECT FUNDS_RPT_HQ.VKBUR, FUNDS_RPT_HQ.VKBUR_NAME, FUNDS_RPT_HQ.FORM_DATE, FUNDS_RPT_HQ.AMT_BEG, FUNDS_RPT_HQ.INGDS, FUNDS_RPT_HQ.INOTH, FUNDS_RPT_HQ.PAYHQ, FUNDS_RPT_HQ.PAYOTH, FUNDS_RPT_HQ.AMT_END, FUNDS_RPT_HQ.TOT_INGDS, FUNDS_RPT_HQ.TOT_PAYHQ, FUNDS_RPT_HQ.FEE_END FROM  FUNDS_RPT_HQ WHERE FUNDS_RPT_HQ.FORM_DATE ='2007-05-09' " ;
   myConn.Open();
   SqlDataAdapter myComm = new SqlDataAdapter(sql, myConn);
  //
   DataSet dset1 = new DataSet();
   myComm.Fill(dset1,"FUNDS_RPT_HQ");
   myConn.Close();

   string ls_rptPathName;
   ls_rptPathName = Server.MapPath(".")+"\\Funds_Rpt_HQ.rpt";
   reportDoc.Load(ls_rptPathName);
   reportDoc.SetDataSource(dset1);
   CrystalReportViewer1.ReportSource = reportDoc;
   CrystalReportViewer1.DataBind();
  }
       
  //匯出檔案操作
  private void Btn_Export2_Click(object sender, System.EventArgs e)
  {      
   try
   {
    reportDataBind();
    string ExportPath;
    string Fname;
    ExportPath = Request.PhysicalApplicationPath + "Exported";
    if ( !Directory.Exists(ExportPath))
    {
     System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "Exported");
    }

    Fname = "Funds_rpt_HQ";

    CrystalDecisions.Shared.DiskFileDestinationOptions opts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
    //匯出為磁碟檔案
    CrystalDecisions.Shared.ExportOptions myExportOptions = reportDoc.ExportOptions;
    myExportOptions.DestinationOptions = opts;
    myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
    //匯出為pdf格式
    reportDoc.ExportOptions.ExportFormatType =  CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
    //目的路徑
    Fname = Fname +".PDF";
    opts.DiskFileName = ExportPath + Fname ;
    //匯出操作
    reportDoc.Export();
    CommonCode.JavaShowWindow("匯出檔案成功!");
   }
   catch(Exception ex)
   {
    CommonCode.JavaShowWindow("匯出檔案失敗!" + ex.Message.ToString().Substring(1,90));
    return ;
   
   }

  }

 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.