active reports动态绑定数据源的问题
查看了下面地址绑定数据源的方法,还是有些疑问,望解答。http://blog.gcpowertools.com.cn/post/2013/05/17/ActiveReports-PageReport-RunTime-DataSource.aspx
以下代码只能放到Page_Load事件下,才能被正确执行
report1.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
如果放在btn1_Click事件下,就不能正确执行,通过代码跟踪,发现没有运行Document_LocateDataSource方法,而是直接跳过了。
请问,如果我要导出报表,如何动态绑定数据源,实现和下面代码类似的效果,我的数据库是MSSql,希望能通过绑定dataset或者datatable的方式来传递数据
string report = "rptInvoice";
GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/Reports/" + report + ".rdlx")));
_reportDef.Report.DataSources.DataSourceReference = "";
_reportDef.Report.DataSources.ConnectionProperties.DataProvider = "OLEDB";
_reportDef.Report.DataSources.ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb")); 补充代码
protected void btnExcel_Click(object sender, EventArgs e)
{
// Provide the page report you want to render.
GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/PageReport1.rdlx")));
_reportDef.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
// Create an output directory
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Provide settings for your rendering output.
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings
excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx;
excelSetting.MultiSheet = false;
GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;
//Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension
excelRenderingExtension = new
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
_reportRuntime.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "inline;filename=自定义.xlsx");
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();
} 您好,
没有运行LocateDataSource方法,需要确定报表是否使用DataSet provider数据源。
使用MS SQL 需要先将数据查询出来,通过DataTable的方式返回给报表。
不需要设置_reportDef.Report.DataSources相关代码。
如何从数据库查询,并返回datatable,需要您自己实现。 protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
return;
}
GrapeCity.ActiveReports.PageReport report1 = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/PageReport1.rdlx")));
report1.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
WebViewer1.PdfExportOptions.FitWindow = false;
WebViewer1.PdfExportOptions.DisplayMode = GrapeCity.ActiveReports.Export.Pdf.Section.DisplayMode.Outlines;
WebViewer1.FlashViewerOptions.ShowSplitter = false;
WebViewer1.Report = report1;
}
void Document_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
{
if (args.DataSourceName == "DataSource1")
{
if (args.DataSetName == "DataSet1")
{
args.Data = GetData();
}
}
}
private DataTable GetData()
{
代码省略……
return dt;
}
通过上面的方法,显示报表内容没有问题,但是导出的时候就不能运行LocateDataSource方法。可以肯定是使用了DatasetProvider数据源
您好,
在web中绑定的方式需要使用onlocatedatasource="WebViewer1_LocateDataSource"。
您可以参考以下例子程序:
看了您发的例子
在web中绑定的方式需要使用onlocatedatasource="WebViewer1_LocateDataSource"。
按照以上方法,可以在查询按钮中运行WebViewer1_LocateDataSource方法,动态传递datatable。测试成功。
但是要在客户端代码上绑定onlocatedatasource才能触发。
但如果是在导出excel的时候,如何绑定onlocatedatasource方法,将数据传递给report呢 您好,
在WebViewer1_LocateDataSource使用
args.Report.Parameters.CurrentValue.ToString();可以拿到参数。
导出的时候,通过拼SQL语句,进行查询。
希望可以帮助到您。 不好意思,刚才可能我描述得不太准确
在显示报表内容的时候可以通过绑定WebView事件onlocatedatasource="WebViewer1_LocateDataSource"。
进入到WebViewer1_LocateDataSource方法获取数据。
但是在导出excel的时候,如何实现类似方法获取数据呢。
前面的代码中:
protected void btnExcel_Click(object sender, EventArgs e)
{
// Provide the page report you want to render.
GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/PageReport1.rdlx")));
_reportDef.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
经测试是没有运行到Document_LocateDataSource方法的。
您提到的
在WebViewer1_LocateDataSource使用
args.Report.Parameters.CurrentValue.ToString();
复制代码
可以拿到参数。
导出的时候,需要在WebViewer1_LocateDataSource方法中添加这条语句
再调用WebViewer1_LocateDataSource方法对吗
这里不是太明白。请问能否提供一个简单的例子,谢谢 导出的时候怎么进入到WebViewer1_LocateDataSource方法中 您好,
导出的时候,参数已经拿到了,可以不进入WebViewer1_LocateDataSource。
我提供一个动态加载报表,并且可以导出excel的例子程序。您可以参考下,看是否满足您的需求。
页:
[1]
2