水晶报表显示数据和 导出Excel、Word、Pdf
2011-09-30 13:14
603 查看
这段时间 接了一个活 开发使用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 } }
相关文章推荐
- 转载 水晶报表导出为word ,excel ,pdf格式
- 水晶报表导出为word,excel和pdf格式(转载)
- 水晶报表导出为word,excel和pdf格式
- 水晶报表(Crystal Report)中的数据导为Excel、PDF、Word等
- 水晶报表导出为pdf, word , excel 格式(已经测试过,实现了该功能)
- 水晶报表导出为pdf, word , excel 格式(已经测试过,实现了该功能)
- 水晶报表导出为pdf, word , excel 格式(已经测试过,实现了该功能)
- 水晶报表导出为word,excel和pdf格式
- 水晶报表导出PDF or Excel,并返回文件到用户的IE浏览器中
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式
- 水晶报表导出PDF or Excel,并返回文件到用户的IE浏览器中
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- FH_1Excel报表导入,显示在JSP页面.人为判断后,不正确的数据生成PDF文档
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- 具有强大构建强大的Word报表,支持XAML数据绑定,文档格式转换,导出为PDF等功能的控件TX Text Control .NET for WPF介绍
- 水晶报表导出为word,excel和pdf格式
- 在.NET环境下将报表数据导出EXCEL和WORD
- [水晶报表]导出PDF or Excel,并返回文件到用户的IE浏览器中
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别