ActiveReports 16 新特性-管理异步或耗时较长的报表渲染
本帖最后由 Lenka.Guo 于 2022-2-22 14:06 编辑你可以管理异步或耗时较长的报表渲染。ActiveReports提供了许多控制输出过程的可能性,有助于您创建响应式应用程序。该方法可适用于以下示例:
[*]以页数显示导出进度(ActiveReports只有在渲染完成后才能显示总页数)。
[*]创建一个带有 "取消 "选项的 "导出 "对话框。
[*]输出有关报表执行的日志信息
显示进度信息
让我们看看如何使用WinForms Viewer 应用程序来显示导出渲染进度信息。导出示例可在github上下载:https://github.com/activereports ... I/PageAndRDL/Export导出PDF、图像、HTML和Excel渲染进度信息可以在报表渲染时显示出来。
注意:如果报表在渲染时,设置画廊模式时,是不会显示导出进度信息。
1. 打开“导出”示例
2. 新建Form窗体,并添加标签控件。
3. 在exportButton_Click()方法中添加以下代码:
C#代码
private async void ExportAsync(PageReport report, IRenderingExtension renderingExtension, StreamProvider outputProvider, NameValueCollection settings)
{
labelExport.Text = "Export started...";
var control = this;
var progress = new Progress<ProgressInfo>(progressInfo =>
{
control.BeginInvoke(new MethodInvoker(() =>
{
labelExport.Text = progressInfo.IsLast ? "Export is finished." : $"Exported {progressInfo.PageNumber} pages.";
))};
});
await Task.Run(() =>
{
try
{
report.Document.Render(renderingExtension, outputProvider, settings, false, false, CancellationToken.None, progress);
}
catch
{
}
});
}
4. 调用 exportButton_Click()方法 代替report.Document.Render() 方法。
5. 运行项目,执行导出按钮,可以看到标签会出现导出的页码信息。
取消报表渲染
代码:
private async void ExportAsync(PageReport report, IRenderingExtension renderingExtension, FileStreamProvider outputProvider, NameValueCollection settings)
{
_cancellationTokenSource = new CancellationTokenSource();
exportButton.Text = "Cancel";
exportButton.Click -= exportButton_Click;
exportButton.Click += cancelExport;
await Task.Run(() =>
{
try
{
report.Document.Render(renderingExtension, outputProvider, settings, false, false, _cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
}
});
if (_cancellationTokenSource.IsCancellationRequested)
MessageBox.Show("Export was cancelled", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information);
_cancellationTokenSource.Dispose();
_cancellationTokenSource = null;
exportButton.Click -= cancelExport;
exportButton.Click += exportButton_Click;
exportButton.Text = "Export";
}
private void cancelExport(object sender, EventArgs e)
{
_cancellationTokenSource?.Cancel();
}
输出导出信息日志
要将报表渲染信息输出日志,可在导出调用之前/之后添加以下代码。如果要了解更多信息,可参考微软的帮助文档: https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/trace-listeners.
C# 代码:
private async void ExportAsync(PageReport report, IRenderingExtension renderingExtension, FileStreamProvider outputProvider, NameValueCollection settings)
{
_cancellationTokenSource = new CancellationTokenSource();
exportButton.Text = "Cancel";
exportButton.Click -= exportButton_Click;
exportButton.Click += cancelExport;
await Task.Run(() =>
{
try
{
report.Document.Render(renderingExtension, outputProvider, settings, false, false, _cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
}
});
if (_cancellationTokenSource.IsCancellationRequested)
MessageBox.Show("Export was cancelled", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information);
_cancellationTokenSource.Dispose();
_cancellationTokenSource = null;
exportButton.Click -= cancelExport;
exportButton.Click += exportButton_Click;
exportButton.Text = "Export";
}
private void cancelExport(object sender, EventArgs e)
{
_cancellationTokenSource?.Cancel();
}
页:
[1]