Print and Export Reports without a Preview
- 5 minutes to read
Users can print or export a report when it is displayed in a Document Viewer. As an alternative, you can print and export reports without having to display them first, as described in this topic.
Create a Report
You can add a report to your application in Visual Studio. See the Create a Report in Visual Studio topic for information on how to do this.
Reports can also be created in application code. See the XtraReport class description for more information.
Print a Report
Use the PrintToolBase.Print method to print a report, as shown in the following code sample:
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System.Drawing.Printing;
// ...
// Create a simple report in code.
XtraReport report = new XtraReport() {
Bands = {
new DetailBand() {
Name = "DetaiBand",
Controls = {
new XRLabel() {
Text = "Simple Report"
}
}
}
}
};
// Print the report.
report.CreateDocument();
PrintToolBase tool = new PrintToolBase(report.PrintingSystem);
tool.Print();
Note
In a server-side application, (for instance, in a web application), the code above sends a report to the default server-side printer.
The Print Reports section describes how to print reports on different platforms.
Export a Report
The XtraReport class exposes methods to export a report to different file formats. Call these methods to export a report from an application that does not have a GUI.
using DevExpress.XtraReports.UI;
// ...
// Create a simple report in code.
XtraReport report = new XtraReport() {
Bands = {
new DetailBand() {
Name = "DetaiBand",
Controls = {
new XRLabel() {
Text = "Simple Report"
}
}
}
}
};
// Export the report to PDF.
//Save the export file to the user's Downloads folder.
report.ExportToPdf(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
@"\Downloads\" + report.Name + ".pdf"
);
The following ASP.NET MVC code sample shows how to allow users to download a report exported to PDF in a web application.
using DevExpress.XtraReports.UI;
using System.Web.Mvc;
using System.IO;
// ...
// Create a simple report in code.
XtraReport report = new XtraReport() {
Bands = {
new DetailBand() {
Name = "DetaiBand",
Controls = {
new XRLabel() {
Text = "Simple Report"
}
}
}
}
};
// Return the report exported to PDF.
MemoryStream ms = new MemoryStream();
report.ExportToPdf(ms);
return File(ms.ToArray(), "application/pdf");
Tip
The following GitHub sample project demonstrates how to print or export reports in an ASP.NET MVC application without a preview: https://github.com/DevExpress-Examples/reporting-print-export-report-without-showing-a-preview.
See Document Export Overview for information on how to export a report to different file formats.
Libraries Required to Print and Export Reports
Add the following libraries to your application to print and export reports. Alternatively, you can add the DevExpress.Reporting.Core NuGet package] NuGet package that includes these core libraries.
Assembly | Description |
---|---|
DevExpress.Data.v20.2.dll | Contains base classes for DevExpress controls. |
DevExpress.Printing.v20.2.Core.dll | Implements basic DevExpress printing features. |
DevExpress.XtraReports.v20.2.dll | Contains the XtraReport class. |
Optional Assembly | Description |
---|---|
DevExpress.Charts.v20.2.Core.dll | Required if a report contains XRChart controls. |
DevExpress.CodeParser.v20.2.dll | Required to process report scripts. |
DevExpress.Data.Desktop.v20.1.dll | Contains base classes for DevExpress controls in .NET Framework and .NET Core. Includes classes that bind data, print, and export content. This library is required if you use the WinControlContainer class to print WinForms controls. |
DevExpress.DataAccess.v20.2.dll | Required when a report is bound to an SQL, Entity Framework, XPO, Excel, JSON, Object, or Federation data source. |
DevExpress.Office.v20.2.Core.dll | Required to export reports to DOCX, or when a report contains XRRichText controls. |
DevExpress.Pdf.v20.2.Core.dll | Required to export reports to PDF. |
DevExpress.Pdf.v20.1.Drawing.dll | Implements PDF-related features. Required to preview reports that are merged with PDF content. |
DevExpress.PivotGrid.v20.2.Core.dll | Required if a report contains XRPivotGrid or XRCrossTab controls. |
DevExpress.RichEdit.v20.2.Core.dll | Required to export reports to DOCX, or when a report contains at least one XRRichText control. |
DevExpress.RichEdit.v20.2.Export.dll | Required to export reports to DOCX. |
DevExpress.Sparkline.v20.2.Core.dll | Required if a report contains XRSparkline controls. |
DevExpress.Xpo.v20.2.dll | Required if a report is bound to an SQL, XPO, or Entity Framework data source. |
DevExpress.XtraCharts.v20.2.dll | Required if a report contains XRChart controls. |