声明:如非注明原创,则本blog所发日志为引用,向原创作者致意!敬礼!
佛曰: 人生最大的财富是健康
佛曰: 人生最大的幸福是放得下
站点公告
最新日志
博文评论
博客留言
博客登陆
博文搜索
博客信息
收藏连接
 
use crystal report in .net(原创)
janedong 发表于 2006/3/22 16:35:00

using System;
using System.Collections;
using System.ComponentModel;
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;
using CrystalDecisions;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
 /// <summary>
 /// Summary description for WebForm1.
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
  protected System.Web.UI.WebControls.Button Button1;
  public CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
 
  private void Page_Load(object sender, System.EventArgs e)
  {     
   string strCrystalPath = ConfigurationSettings.AppSettings["crystalPath"] + "\\CrystalReport1.rpt";
   oRpt.Load(strCrystalPath);
   
   string sConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

   SqlConnection sqlConn = new SqlConnection(sConnectionString);
   SqlCommand sqlComm = new SqlCommand();
   SqlDataAdapter dataAdapter = new SqlDataAdapter();

   sqlConn.Open();

   sqlComm.Connection = sqlConn;
   sqlComm.CommandType = CommandType.Text;

   dataAdapter.SelectCommand = sqlComm;

   DataSet dataSet = new DataSet();

   string sSQL = "SELECT * FROM masterT";

   sqlComm.CommandText = sSQL;
   dataAdapter.Fill(dataSet,"masterT");

   sSQL = "SELECT * FROM sub";

   sqlComm.CommandText = sSQL;
   dataAdapter.Fill(dataSet,"sub");

   CrystalDecisions.CrystalReports.Engine.ReportDocument sub = oRpt.OpenSubreport("sub");
   sub.SetDataSource(dataSet);
   oRpt.SetDataSource(dataSet);

   CrystalReportViewer1.DisplayToolbar = false;
   CrystalReportViewer1.ReportSource = oRpt;
   CrystalReportViewer1.Visible = true;

   sqlConn.Close();

  }

  #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.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   CrystalDecisions.Shared.ExportOptions myExportOptions ;   
   CrystalDecisions.Shared.DiskFileDestinationOptions myDiskFileDestinationOptions;
   string myExportFile = ConfigurationSettings.AppSettings["crystalOutputPath"];
   if ( ! System.IO.Directory.Exists( myExportFile ) )
   {
    System.IO.Directory.CreateDirectory( myExportFile );
   }
   string strType = "doc";
   switch (strType)
   {
    case "doc":
     myExportFile = myExportFile + "\\a.doc";
     break;
    case "pdf":
     break;
    case "xsl":
     break;
   }

   myDiskFileDestinationOptions = new  CrystalDecisions.Shared.DiskFileDestinationOptions();           
   myDiskFileDestinationOptions.DiskFileName = myExportFile;
   myExportOptions = oRpt.ExportOptions;

   myExportOptions.DestinationOptions = myDiskFileDestinationOptions;

   myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;

   myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;

   oRpt.Export();

   Response.ClearContent();
   Response.ClearHeaders();
   Response.ContentType = "application/vnd.ms-word";

   Response.Redirect(myExportFile, false );
   Response.Flush();
   Response.Close();

  }

 }
}

发表评论:

    昵称:
    密码:
    主页:
    标题: