水晶报表显示数据和 导出Excel、Word、Pdf

这段时间 接了一个活  开发使用vs2003和sql2000 后来修改报表时 总是出现错误  在网上也没有找到具体的办法 

呵呵  幸不辱命 终于搞定了

贴出来供以后有需要的童鞋参考

 没有废话,直接上代码:

namespace SCACLMS.NewPages
{
 /// <summary>
 /// EveryMonthRep 的摘要说明。
 /// </summary>
 public class EveryMonthRep :ReportBase // System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DropDownList ddlYear;
  protected System.Web.UI.WebControls.DropDownList ddlMonth;
  protected System.Web.UI.WebControls.Button Search;
  protected System.Web.UI.WebControls.DropDownList ddlFile;
  protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
  protected System.Web.UI.WebControls.DropDownList Drpdept;
  protected System.Web.UI.WebControls.Button btnExport; 
  protected CrystalReport3 repDoc =new CrystalReport3();
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!IsPostBack)
   {
    Inits();
    DBinDepart();
    this.CrystalReportViewer1.Visible=false;
//    RptDoc = repDoc;
//    SetDbLoginInfo();
   }
  }

  /// <summary>
  /// 绑定部门
  /// </summary>
  void DBinDepart()
  {
   SCACLMS.BLL.Student bll = new SCACLMS.BLL.Student();
   DataSet ds= bll.GetDepartList();
   DataTable dt= ds.Tables[0];  
   this.Drpdept.DataSource=dt;
   this.Drpdept.DataValueField="DepartId";
   this.Drpdept.DataTextField="Title";
   this.Drpdept.DataBind(); 
  } 
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  } 
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Search.Click += new System.EventHandler(this.Search_Click);
   this.btnExport.Click += new System.EventHandler(this.btnExport_Click);
   this.Load += new System.EventHandler(this.Page_Load);  }
  #endregion
  #region 初始化数据
  private void Inits()
  {
   InitDdl();

//   RptDoc = repDoc;
//   SetDbLoginInfo();
  }
  
  private void InitDdl()
  {
   ddlYear.Items.Clear();
   for(int i=2007;i<=2020;i++)
   {
    ddlYear.Items.Add(new ListItem(i.ToString(),i.ToString()));
   }
   ddlMonth.Items.Clear();
   for (int j=1;j<=12;j++)
   {
    ddlMonth.Items.Add(new ListItem(j.ToString(),j.ToString()));
   }
   ddlYear.SelectedValue = Convert.ToString(DateTime.Now.Year);
   ddlMonth.SelectedValue = Convert.ToString(DateTime.Now.Month);
  }
  #endregion

  private void Search_Click(object sender, System.EventArgs e)
  {
   string StrDep=this.Drpdept.SelectedItem.Text;
   int Iyear=int.Parse(this.ddlYear.SelectedItem.Text.ToString());
   int IMonth=int.Parse(this.ddlMonth.SelectedItem.Text.ToString());
      //显示数据
   SCACLMS.BLL.Student bll = new SCACLMS.BLL.Student();
   DataSet dsAll= bll.SelectEdutinaInfo(Iyear,IMonth,StrDep);
   DataTable dt= dsAll.Tables[0];    
   if(dt.Rows.Count>0)
   {  
    CrystalReportViewer1.Visible = true;
    repDoc.SetDataSource(dt);
    RptDoc = repDoc;
    SetDbLoginInfo();
    this.CrystalReportViewer1.ReportSource=repDoc;
    CrystalReportViewer1.DisplayGroupTree = false; //树视图是可见还是隐藏左边的空白显示的比较多的处理方法  
   }
    }
/*导出Excel word pdf  具体数据源怎么写 可以根据实际情况修改  以下情况 仅供参考*/ 
  private void btnExport_Click(object sender, System.EventArgs e)
  {
   string StrDep=this.Drpdept.SelectedItem.Text;
   int Iyear=int.Parse(this.ddlYear.SelectedItem.Text.ToString());
   int IMonth=int.Parse(this.ddlMonth.SelectedItem.Text.ToString());
   SCACLMS.BLL.Student bll = new SCACLMS.BLL.Student();
   DataSet dsAll= bll.SelectEdutinaInfo(Iyear,IMonth,StrDep);
   DataTable dt= dsAll.Tables[0];    
   if(dt.Rows.Count>0)
   { 
    string StrGS=ddlFile.SelectedValue;
    CrystalReportViewer1.DisplayGroupTree = true;
    repDoc.SetDataSource(dt);
    RptDoc = repDoc;
    SetDbLoginInfo();
    ExportReport(StrGS);  
   }
  }
 }
}

 

namespace SCACLMS.ReportForms
{
 /// <summary>
 /// ReportBase 的摘要说明。
 /// </summary>
 public class ReportBase: BasePage
 {
  private ReportDocument mDoc;
  private string _DbServer = "(local)";
  private string _DbUser  = "sa";
  private string _DBPwd  = "";
  private string _DbDatabase = "SY_asd";
  public ReportBase(): base()
  {
   this.Init  += new EventHandler( this.basePage_Init );
   this.Load  += new EventHandler( this.basePage_Load );
   this.PreRender += new EventHandler( this.basePage_PreRender );
  }  
  #region 初始化事件
  private void basePage_Init(object sender, EventArgs e)
  {
  }

  private void basePage_Load(object sender, EventArgs e)
  {
  }

  private void basePage_PreRender(object sender, EventArgs e)
  {
  }
  #endregion

  #region 报表导出
  protected void  ExportReport(string   OptionStr)
  {
   ExportReport((ReportClass)RptDoc,OptionStr);
  }

  /// <summary>
  /// 导出报表
  /// </summary>
  /// <param name="Report">报表</param>
  /// <param name="OptionStr">导出文件类型doc,pdf,xls</param>
  protected void ExportReport(ReportClass Report,string   OptionStr)  
  {  
   string   ExportPath;  
   ExportPath   =   Request.PhysicalApplicationPath   +   "Exported\\";  
   if   (Directory.Exists(ExportPath)   ==   false) 
   Directory.CreateDirectory(Request.PhysicalApplicationPath   +   "Exported\\");  
   ReportDocument   crReportDocument   =   (ReportDocument)Report;  
   DiskFileDestinationOptions   crDiskFileDestinationOptions   =   new DiskFileDestinationOptions();  
   ExportOptions   crExportOptions   =   crReportDocument.ExportOptions;  
   
   switch(OptionStr.ToUpper())  
   {  
    case   "PDF":  
     crDiskFileDestinationOptions.DiskFileName   =   ExportPath   +   "PortableDoc.pdf";  
     crExportOptions.DestinationOptions   =   crDiskFileDestinationOptions;  
     crExportOptions.ExportDestinationType   =   ExportDestinationType.DiskFile;  
     crExportOptions.ExportFormatType   =   ExportFormatType.PortableDocFormat;  
     break;  
    case   "DOC":  
     crDiskFileDestinationOptions.DiskFileName   =   ExportPath   +   "Word.doc";  
     crExportOptions.ExportDestinationType   =   ExportDestinationType.DiskFile;  
     crExportOptions.ExportFormatType   =   ExportFormatType.WordForWindows;  
     crExportOptions.DestinationOptions   =   crDiskFileDestinationOptions;  
     break; 
    case   "XLS":
     crDiskFileDestinationOptions.DiskFileName   =   ExportPath   +   "Excel.xls";  
     crExportOptions.ExportDestinationType   =   ExportDestinationType.DiskFile;  
     crExportOptions.ExportFormatType   =   ExportFormatType.Excel;  
     crExportOptions.DestinationOptions   =   crDiskFileDestinationOptions;  
     break;
   }
   try  
   {  
    TableLogOnInfo logInfo = GetLogInfo();
    crReportDocument.Database.Tables[0].ApplyLogOnInfo(logInfo);    
    //   Export   the   report  
    crReportDocument.Export();  
    crReportDocument.Close();  
    Response.ClearContent();  
    Response.ClearHeaders();  
    switch(OptionStr.ToUpper())  
    {  
     case   "PDF":  
      Response.ContentType="application/pdf";  
      string sFileName2 = System.IO.Path.GetFileName(Request.PhysicalPath);
      sFileName2 = sFileName2.Replace("aspx","pdf");
      Response.AddHeader("Content-Disposition", ("attachment;Filename="+sFileName2));
      Response.WriteFile(ExportPath   +   "PortableDoc.pdf");  
      break;  
     case   "DOC":
      Response.ContentType="application/msword";
      string sFileName1 = System.IO.Path.GetFileName(Request.PhysicalPath);
      sFileName1 = sFileName1.Replace("aspx","doc");
      Response.AddHeader("Content-Disposition", ("attachment;Filename="+sFileName1));
      Response.WriteFile(ExportPath   +   "Word.doc");  
      break;  
     case   "XLS":
      Response.ContentType="application/xls";
      string sFileName = System.IO.Path.GetFileName(Request.PhysicalPath);
      sFileName = sFileName.Replace("aspx","xls");
      Response.AddHeader("Content-Disposition", ("attachment;Filename="+sFileName));
      Response.WriteFile(ExportPath   +   "Excel.xls");  
      break;  
    }
    Response.Flush();  
    Response.Close();
   }  
   catch   (Exception   err)  
   {  
    Response.Write("<BR>");  
    Response.Write(err.Message.ToString());  
   }  
  }  
  #endregion
  #region 系统设置
  /// <summary>
  /// 报表Doc文件
  /// </summary>
  public ReportDocument RptDoc
  {
   set
   {
    mDoc = value;
    SetDbLoginInfo();
   }
   get
   {
    return mDoc;
   }
  }
  /// <summary>
  /// 设置登录信息
  /// </summary>
  protected void SetDbLoginInfo()
  {
   TableLogOnInfo logInfo = GetLogInfo();
   RptDoc.Database.Tables[0].ApplyLogOnInfo(logInfo);
  }
  private TableLogOnInfo GetLogInfo()
  {
   GetDbPara();
   //设置logOnInfo参数
   TableLogOnInfo   logOnInfo    =   new   TableLogOnInfo();
   logOnInfo.ConnectionInfo.ServerName  = _DbServer;
   logOnInfo.ConnectionInfo.DatabaseName   =   _DbDatabase;
   logOnInfo.ConnectionInfo.UserID   =   _DbUser;
   logOnInfo.ConnectionInfo.Password  =   _DBPwd;
   return logOnInfo;
  }
  /// <summary>
  /// 获取数据库相关信息
  /// </summary>
  private void GetDbPara()
  {
   string str = SCACLMS.Common.Config.connectionString;
   string[] para = str.Split(';');
   if (para.Length>=4)
   {
    foreach (string s in para)
    {
     if (s.IndexOf("server")==0)
     {
      _DbServer = s.Replace("server=","").Replace(";","");
     }else
     if(s.IndexOf("pwd")==0)
     {
      _DBPwd = s.Replace("pwd=","").Replace(";","");
     }else
     if (s.IndexOf("uid")==0)
     {
      _DbUser = s.Replace("uid=","").Replace(";","");
     }else
     if (s.IndexOf("database")==0)
     {
      _DbDatabase = s.Replace("database=","").Replace(";","");
     }
    }
   }   
  }
  #endregion
 }
}

 

 

 

转载于:https://www.cnblogs.com/dain/archive/2011/09/30/2196455.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值