为报表数据集填充数据:
//strProvider为连接字符串
string strProvider = "Server=(local);DataBase=pubs;UID=sa;PWD=";
CrystalReport1 oCR = new CrystalReport1();
Dataset1 ds = new Dataset1();
SqlConnection MyConn = new SqlConnection(strProvider);
MyConn.Open(); string strSel = "Select * from Stores";
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,"stores");
oCR.SetDataSource(ds);
this.CrystalReportViewer1.ReportSource = oCR;
使用pull模式导出报表:
你能够将报表文件导出成为下列格式:
1. PDF (Portable Document Format)
2. DOC (MS Word Document)
3. XLS (MS Excel Spreadsheet)
4. HTML (Hyper Text Markup Language – 3.2 or 4.0 compliant)
5. RTF (Rich Text Format)
代码如下:
{
CrystalReport1 myReport= New CrystalReport1() '注意:这里我们建立一个strong-typed的水晶报表实例。
CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts= New CrystalDecisions.Shared.DiskFileDestinationOptions()
myReport.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile ' 导出成为其它文件时也需要这个选项 ' 如Microsoft Exchange, MAPI等.
myReport.ExportOptions.ExportFormatType = CrystalDecisions. [Shared].ExportFormatType.PortableDocFormat '这里我们导出成为.pdf格式文件,你也能选择上面的其它类型文件
DiskOpts.DiskFileName = "c:/Output.pdf" '如果你不指定确切的目录,那么文件就会保存到[Windows]/System32目录中去了
myReport.ExportOptions.DestinationOptions = DiskOpts '水晶报表文件不包含直接的FileName属性,因此你不能直接指定保存的文件名 '所以你不得不使用DiskFileDestinationOptions对象,设置它的DiskFileName属性 '为你想要的路径,最后将水晶报表的DestinationsOptions属性指定为上面的DiskFileDestinationOption
myReport.Export() '上面的代码将完成导出工作。
}
使用PUSH模式导出水晶报表:
当导出的报表是由PUSH模式建立的时,第一步就是通过编程建立连接并组装DataSet,设置报表的的SetDataSource属性。再下面的步骤就有Pull模式一样的了。