目录
- 打印
- 文档预览
- 快速开始
- GUI
- API和自定义
- 报表设计器
- 快速开始
- GUI
- API和自定义
- 应用安全
- 安全加载
- 数据安全访问
- UI显示
- 修改应用主题
- 修改图标,位图、适量图
- 本地化显示
- 本地化打印预览
- 本地化报表设计器
本系列教程基于DevExpress 官方帮助文档。帮助文档下载地址
本文是针对WPF的文档,当然你也可以用于WInform。
基本方法
你可以使用PrintHelper类打印Report。
PrintHelpe.PrintDirect方法使用默认打印设置将报表发送到打印机。此方法提供重载,允许您使用系统的默认打印机或特定打印,显示的UI如下
打印一个简单报表示例
using DevExpress.Xpf.Printing;
// ...
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) {
XtraReport1 report = new XtraReport1();
// Invoke the Print dialog.
PrintHelper.Print(report);
// Send the report to the default printer.
PrintHelper.PrintDirect(report);
// Send the report to the specified printer.
PrintHelper.PrintDirect(report, "myPrinter");
}
XtraReport1 是一个设计的自定义报表。你可以用VS 设计,也可以报表设计器设计。
调用这几个方法会自动创建一个文档(文档不存在的情况下),自动调用的方法是 XtraReport.CreateDocument。
如果你要将多个报表一起打印(对于打印机来说就是一个任务),你需要将这个报表合并到一个文档中。具体操作请参考合并报表页面
打印大型报表
使用CachedReportSource组件打印包含大量数据的报表。此组件允许您在文档生成期间将页面存储在文件系统或数据库中,从而避免内存消耗过度。
示例代码如下
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting.Caching;
//...
var storage = new MemoryDocumentStorage();
var report = new XtraReport1();
var cachedReportSource = new CachedReportSource(report, storage);
在代码中使用的MemoryDocumentStorage可以将文档紧凑地存储在内存中。当然你也可以使用FileDocumentStorage、DbDocumentStorage或实现自定义存储。
示例代码如下
using DevExpress.Xpf.Printing;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting.Caching;
// ...
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) {
var storage = new MemoryDocumentStorage();
var report = new XtraReport1();
var cachedReportSource = new CachedReportSource(report, storage);
// Invoke the Print dialog.
PrintHelper.Print(cachedReportSource);
// Send the report to the default printer.
PrintHelper.PrintDirect(cachedReportSource);
// Send the report to the specified printer.
PrintHelper.PrintDirect(cachedReportSource, "myPrinter");
}