110,534
社区成员
发帖
与我相关
我的任务
分享
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;
}
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
}
}
//报表
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();