很久没有弄过水晶报表了,以前弄的。using System; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using System.Data.SqlClient; using System.Data; using System.Collections; using System.Windows.Forms; using System.IO;namespace test_dx { /// <summary> /// report 的摘要说明。 /// </summary> public class report { private ReportDocument rdc; private DiskFileDestinationOptions disk; private ExportOptions ex; public report() { disk = new DiskFileDestinationOptions(); ex = new ExportOptions(); // // TODO: 在此处添加构造函数逻辑 // } #region//得到各种格式水晶报表,参数:数据集、导出文件格式、保存导出文件的完整路径。 public void getCrystal(DataSet ds,string type,string path) //参数:数据集、导出文件格式、导出文件的完整路径。 { try { //set ReportDocument; rdc = new ReportDocument(); rdc.Load(Application.StartupPath + @"\Report1.rpt" ); rdc.SetDataSource(ds);
//set path; disk.DiskFileName = @path; ex = rdc.ExportOptions; ex.DestinationOptions = disk; ex.ExportDestinationType = ExportDestinationType.DiskFile; //Export the report switch (type) { case "pdf": ex.ExportFormatType = ExportFormatType.PortableDocFormat; break; case "doc": ex.ExportFormatType = ExportFormatType.WordForWindows; break; case "xls": ex.ExportFormatType = ExportFormatType.Excel; break; case "txt": ex.ExportFormatType = ExportFormatType.Text; break; default: this.writeLog("Export type error!"); break; }
payReport pay_Report = new payReport()
實例化後怎麼顯示這個實例呢? pay_Report好像沒有.show ?
功能和水晶报表类似。
水晶报表要注册。
给你一个我写的代码: //报表
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetReport_数据集", DataTable));
ReportViewer1.LocalReport.ReportPath = "Report/报表(带路径).rdlc";
ReportViewer1.LocalReport.ReportEmbeddedResource = "报表.rdlc";
//参数
ReportParameter rpStartDate = new ReportParameter("StartDate", StartDate.ToShortDateString());
ReportParameter rpEndDate = new ReportParameter("EndDate", EndDate.ToShortDateString());
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rpStartDate, rpEndDate });
ReportViewer1.DataBind();
ReportViewer1.LocalReport.Refresh();
再 report.Load(Application.StartupPath + "\\Report\\" + _reportName + ".rpt"); //load报表文件
report.SetDataSource(yourDataSet);
report.SetParameterValue("parameterName", "parameterValue");//设置报表变量
再用CrystalReportViewer这个控件
crystalReportViewer1.ReportSource = report;
crystalReportViewer1.Zoom(1);
crystalReportViewer1.Show();
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using System.IO;namespace test_dx
{
/// <summary>
/// report 的摘要说明。
/// </summary>
public class report
{
private ReportDocument rdc;
private DiskFileDestinationOptions disk;
private ExportOptions ex;
public report()
{
disk = new DiskFileDestinationOptions();
ex = new ExportOptions();
//
// TODO: 在此处添加构造函数逻辑
//
} #region//得到各种格式水晶报表,参数:数据集、导出文件格式、保存导出文件的完整路径。
public void getCrystal(DataSet ds,string type,string path) //参数:数据集、导出文件格式、导出文件的完整路径。
{
try
{
//set ReportDocument;
rdc = new ReportDocument();
rdc.Load(Application.StartupPath + @"\Report1.rpt" );
rdc.SetDataSource(ds);
//set path;
disk.DiskFileName = @path;
ex = rdc.ExportOptions;
ex.DestinationOptions = disk;
ex.ExportDestinationType = ExportDestinationType.DiskFile; //Export the report
switch (type)
{
case "pdf":
ex.ExportFormatType = ExportFormatType.PortableDocFormat;
break;
case "doc":
ex.ExportFormatType = ExportFormatType.WordForWindows;
break;
case "xls":
ex.ExportFormatType = ExportFormatType.Excel;
break;
case "txt":
ex.ExportFormatType = ExportFormatType.Text;
break;
default:
this.writeLog("Export type error!");
break;
}
rdc.Export();
}
catch(Exception ex)
{
this.writeLog(ex.ToString());
}
}
#endregion #region//得到报表ReportDocument,参数:数据集、水晶报表框架路径。
public ReportDocument getPDF(DataSet ds,string reportPath)
{
try
{
rdc = new ReportDocument();
rdc.Load(reportPath);
rdc.SetDataSource(ds);
}
catch(Exception ex)
{
this.writeLog(ex.ToString());
}
return rdc;
}
#endregion #region//写日志
private void writeLog(string ex) //报表日志
{
try
{
string path = Application.StartupPath + @"\log\pdflog.txt";
string time = DateTime.Now.ToString();
StreamWriter sw = new StreamWriter(path,true,System.Text.Encoding.UTF8);
sw.WriteLine(time);
sw.WriteLine(ex);
sw.WriteLine(" ");
sw.Flush();
sw.Close();
}
catch(Exception ss)
{
this.writeLog(ss.ToString());
}
finally
{
MessageBox.Show("Report log test ok!","TEST");
}
}
#endregion
}
}
http://topic.csdn.net/u/20081118/10/49a1751d-1d86-44e4-969f-a847c2316dd3.html0基础,看完就会了。