大众计算机学习网欢迎诸位!收藏本站注 册登 陆
常用教程:基础知识网络知识操作系统WPS网页制作数据库算法网页成功之路网络安全最新技术古典mid流行midmid背景下载中心
您现在的位置:首页 > 教程 > 文章页

下篇—水晶报表导出为word/excel文件的代码

Admin | 2009-6-8 11:30:37 | ReadNums | 6493 | 标签 asp.net | 打印本页
     

   //绑定数据源
   private void reportDataBind()
   {
      SqlConnection myConn = new SqlConnection("server = 11.101.10.8; database =eProcessDB; uid = sa; pwd =admin@sa ");
      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.DisplayGroupTree = false; //树视图是可见还是隐藏左边的空白显示的比较多的处理方法
  
   }
   private void btn_Export2_Click(object sender, System.EventArgs e)
   {
       reportDataBind();
  
       string contenttype = "";
       string ls_FileType;
       ls_FileType = DropDownList2.SelectedValue;

                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;

       switch(DropDownList2.SelectedItem.Value)
       {
        case "PDF":
         contenttype = "application/pdf";
         myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
         break;
      
        case "DOC":
         Response.ContentType = "application/ms-excel";
         myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
         break;
      
        case "XLS":
         contenttype = "application/vnd.ms-excel";
         this.EnableViewState = false;
         myExportOptions.ExportFormatType =CrystalDecisions.Shared.ExportFormatType.Excel;
         break;
       }
              
               Fname = Fname +"." + DropDownList2.SelectedItem.Value;
               opts.DiskFileName = ExportPath + Fname ;
                //导出操作
               reportDoc.Export();
               CommonCode.JavaShowWindow("导出文件成功!");
   }

}
}


问题未解决:在线咨询

网友评论

(访客)
内容实用原创,讲得很好。
20xx年x月x日
(站长)
有问题请在线咨询。
20xx年x月x日