我总是将数据集绑定到报告方式
string exeFolder = Path.GetDirectoryName(Application.StartupPath);
string reportPath = Path.Combine(GetRootPath(), @"Report1.rdlc");
DataTable dt = GetDataSet();
List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("ReportParameter1", "Hello"));
reportViewer1.Reset();
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dsJobs", dt));
reportViewer1.LocalReport.ReportPath = reportPath;
reportViewer1.LocalReport.SetParameters(parameters);
reportViewer1.RefreshReport();
so want to know can i bind list like
public class Person
{
public string Name1{get; set;}
public string Address{get; set;}
public string Email{get; set;}
}
List<Person> p = new List<Person>();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(p));
请指导我。感谢
答案 0 :(得分:4)
我创建了一个类,可以帮助您模拟报告的页面代码隐藏类:
public abstract class ReportGenerator
{
private readonly List<ReportParameter> _reportParameters;
private readonly List<ReportDataSource> _reportDataSources;
private readonly LocalReport _report;
private readonly string _friendlyName;
private IList<ReportGenerator> _subReports;
private readonly string _repName;
internal string ReportName
{
get { return _repName; }
}
public List<ReportDataSource> ReportDataSources
{
get { return _reportDataSources; }
}
public List<ReportParameter> ReportParameters
{
get { return _reportParameters; }
}
public abstract void SetParamsAndDataSources();
protected ReportGenerator(string friendlyName, string reportName)
{
_friendlyName = friendlyName;
_report = new LocalReport { ReportPath = string.Format("Reports/{0}.rdlc", reportName) };
_repName = reportName;
_reportParameters = new List<ReportParameter>();
foreach (var paramIter in _report.GetParameters())
{
_reportParameters.Add(new ReportParameter(paramIter.Name));
}
_reportDataSources = new List<ReportDataSource>();
foreach (var dsName in _report.GetDataSourceNames())
{
_reportDataSources.Add(new ReportDataSource { Name = dsName });
}
}
private void report_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
if(_subReports == null)
{
throw new ApplicationException(string.Format("Report '{0}' has subreport(s) which are not properly set up.", _repName));
}
var srLst = _subReports.Where(srIter => srIter.ReportName.Equals(e.ReportPath)).ToList();
if (!srLst.Any())
{
return;
}
var srFound = srLst[0];
foreach (var dsIter in srFound.ReportDataSources)
{
e.DataSources.Add(dsIter);
}
}
public void RefreshReportViewer(out ReportViewer rvw)
{
InitReport();
rvw = new ReportViewer();
rvw.LocalReport.ReportPath = _report.ReportPath;
rvw.LocalReport.SetParameters(ReportParameters);
foreach (var dsIter in ReportDataSources)
{
rvw.LocalReport.DataSources.Add(dsIter);
}
rvw.LocalReport.SubreportProcessing += report_SubreportProcessing;
rvw.LocalReport.Render("Word");
}
public byte[] GenerateReport(Formats reportFormat, out string mimeType, out string fileName)
{
SetParamsAndDataSources();
ValidateReportParameters();
ValidateReportDataSources();
_report.SetParameters(_reportParameters);
foreach (var dsIter in _reportDataSources)
{
_report.DataSources.Add(dsIter);
}
_report.SubreportProcessing += report_SubreportProcessing;
Warning[] warnings;
string[] streamIds;
string encoding;
string extension;
fileName = string.Format("{0}_{1}.", _friendlyName, DateTime.Now.ToString("ddMMyyhhmmss"));
var repExp = _report.Render(reportFormat.ToString("G"), null, out mimeType, out encoding, out extension, out streamIds, out warnings);
fileName += extension;
return repExp;
}
private void InitReport()
{
SetParamsAndDataSources();
ValidateReportParameters();
ValidateReportDataSources();
_report.SetParameters(_reportParameters);
foreach (var dsIter in _reportDataSources)
{
_report.DataSources.Add(dsIter);
}
}
protected void SetSubReports(params ReportGenerator[] subReports)
{
_subReports = subReports.ToList();
}
private void ValidateReportParameters()
{
var paramsMetaData = _report.GetParameters();
foreach (var paramIter in _reportParameters.Where(paramIter => paramIter.Values.Count == 0 && !paramsMetaData[paramIter.Name].AllowBlank))
{
throw new ApplicationException(string.Format(
"Report {0}: Value not supplied for the parameter: {1}", _report.ReportPath, paramIter.Name));
}
}
private void ValidateReportDataSources()
{
foreach (var dsIter in _reportDataSources.Where(paramIter => paramIter.Value == null))
{
throw new ApplicationException(string.Format("Report {0}: Value not supplied for the DataSource: {1}", _report.ReportPath, dsIter.Name));
}
}
internal void SetReportParameterValue(string paramName, params string[] paramValues)
{
foreach (var paramIter in _reportParameters.Where(paramIter => paramIter.Name.Equals(paramName)))
{
paramIter.Values.AddRange(paramValues.ToArray());
return;
}
}
internal void SetDataSourceValue(string dataSourceName, object dataSourceValue)
{
foreach (var dsIter in _reportDataSources.Where(dsIter => dsIter.Name.Equals(dataSourceName)))
{
dsIter.Value = dataSourceValue;
return;
}
}
public enum Formats
{
PDF = 1,
Excel,
Word
}
}
报告的示例代码隐藏:
public class YourReport: ReportGenerator
{
public string Parameter1 { get; set; }
public string Parameter2 { get; set; }
public string Parameter3 { get; set; }
public string SubReportParameter1 { get; set; }
public string SubReportParameter2 { get; set; }
public List<SomeEntity> EntityList { get; set; }
public List<SomeOtherEntity> SubReportEntityList { get; set; }
private readonly CodeBehindClassForAnySubReport _anySubReport;
public YourReport():base("ReportName", "ReportFileName")
{
_anySubReport = new YourReport();
SetSubReports(_anySubReport);
}
public override void SetParamsAndDataSources()
{
SetReportParameterValue("Param1NameInReport", Parameter1);
SetReportParameterValue("Param2NameInReport", Parameter2);
SetReportParameterValue("Param3NameInReport", Parameter3);
SetReportParameterValue("SubParam1NameInReport", SubReportParameter1);
SetReportParameterValue("SubParam2NameInReport", SubReportParameter2);
base.SetDataSourceValue("DataSourceNameInReport", EntityList);
_anySubReport.EntityList = this.SubReportEntityList;
//Call this only when subreport contains any datasource.
//Parameters for subreports should be chained in the report design
_anySubReport.SetParamsAndDataSources();
}
}
前端代码隐藏类的示例用法:
var repGen = new YourReport
{
Parameter1 = someValue1,
Parameter2 = someValue2,
Parameter3 = someValue3,
SubReportParameter1 = someValue4,
SubReportParameter2 = someValue5,
EntityList = someList1,
SubReportEntityList = someList2
};
var repExp = repGen.GenerateReport(format, out mimeType, out fileName);
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(repExp);
Response.Flush();
报告生成器是一个库,它像MS-Reports的代码隐藏一样。它使报告导出易于在Web层中使用。
对于任何新报告,请按照以下步骤模拟代码隐藏类:
按照以下简单步骤在网络图层中使用创建的新课程:
是
是的,很多。其中很少是: 1.向ReportGenerator基类添加一个方法,该方法可以将报表附加到报表查看器。 2.目前,基类将所有报告参数视为“string”类型。可以修改以适应任何类型的参数。