正文

水晶报表导出Excel(二)2007-03-28 14:57:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/mimengjiangnan/24350.html

分享到:

水晶报表自带的导出Excel不太完美,导出的文件没有网格线,要使它出现网格线就要自己写导出方法.要注意的一点是只有用强类型的水晶报表才可以写自定义的导出方法(个人意见)。方法步骤: 1,  创建个数据集 2,  在业务层创建水晶报表文件(强类型水晶报表就是把水晶报表放在项目里面,而不是网站里,这样在界面可以调用报表文件相关的方法) 3,  报表绑定: 在方法前面声明必要的参数     ReportDocument ReportDoc;     TableLogOnInfo logOnInfo;     DiskFileDestinationOptions FileOPS;     ExportOptions ExOPS;     CrystalReport1 cr = new CrystalReport1(); 绑定代码:             DataSet1 ds = new DataSet1();             SqlConnection con = new SqlConnection("server=YWG;uid=sa;pwd=;database=jcjyzbxj");             SqlDataAdapter da = new SqlDataAdapter("SELECT DM,MC FROM DM_XB", con);             da.Fill(ds, "DM_XB");             cr.SetDataSource(ds);             CrystalReportViewer1.ReportSource = cr; 导出Excel代码: 调用Excel的方法:     protected void Button1_Click(object sender, EventArgs e)     {         DataSet1 ds = new DataSet1();         SqlConnection con = new SqlConnection("server=YWG;uid=sa;pwd=;database=jcjyzbxj");         SqlDataAdapter da = new SqlDataAdapter("SELECT DM,MC FROM DM_XB", con);         da.Fill(ds, "DM_XB");         cr.SetDataSource(ds);         CrystalReportViewer1.ReportSource = cr;           string ReportFile = Server.MapPath("CrystalReport1.rpt");         string ExcelFileName = "g:\\myExcel.xls";           bool msg= ExportToExcel(ReportFile, ds, ExcelFileName);             } 导出Excel的方法: public bool ExportToExcel(string ReportFile, object ReportDataSource, string ExcelFileName)     {         try         {             ReportDoc = new ReportDocument();             logOnInfo = new TableLogOnInfo();             FileOPS = new DiskFileDestinationOptions();             cr.Load(ReportFile);             cr.SetDataSource(ReportDataSource);             FileOPS.DiskFileName = ExcelFileName;             ExOPS = cr.ExportOptions;             ExOPS.DestinationOptions = FileOPS;             ExOPS.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;             ExOPS.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.ExcelRecord;             cr.Export();             System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;             Response.ClearContent();             Response.ClearHeaders();             //Response.ContentType = contentType;             //Response.WriteFile(ExcelFileName);             Response.Flush();             Response.Close();                 return true;         }         catch         {             return false;         }     } 需要注意的地方:1在导出Excel以前一定要重新绑定一次数据,要不导出的文件没有数据 2导出Excel的文件类型要是ExcelRecord,不能是Excel否则一样没有网格线  

阅读(9149) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册