xtrareports-2578-feature-guide-to-devexpress-reports-store-and-distribute-reports-export-reports-export-to-csv.md
This document describes how to export a report document to CSV format.
You can export a report from the Report Designer’s Preview and the Document Viewer on all supported platforms (WinForms, WPF, ASP.NET Web Forms, ASP.NET MVC, and ASP.NET Core). To export a file, expand the Export Document drop-down list and select CSV File. Specify export options in the invoked dialog and click OK.
To export a report document to CSV format in code, use one of the following techniques:
Call the XtraReport.ExportToCsv overloaded method to export a report. To export a report asynchronously in a separate task, call the XtraReport.ExportToCsv overloaded method instead. To specify export options, create the CsvExportOptions class instance and pass this instance to the invoked method, or use the ExportOptions.Csv property.
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using DevExpress.Utils;
// ...
public bool ExportReport(XtraReport report, string filename) {
try {
// Specify CSV export options.
var options = new CsvExportOptions();
options.SkipEmptyColumns = false;
options.SkipEmptyRows = false;
options.EncodeExecutableContent = DefaultBoolean.True;
// Export a report to CSV.
report.ExportToCsv(filename, options);
return true;
}
// Return false if the CSV export failed.
catch {
return false;
}
}
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.Utils
' ...
Public Function ExportReport(ByVal report As XtraReport, ByVal filename As String) As Boolean
Try
' Specify CSV export options.
Dim options = New CsvExportOptions()
options.SkipEmptyColumns = False
options.SkipEmptyRows = False
options.EncodeExecutableContent = DefaultBoolean.True
' Export a report to CSV.
report.ExportToCsv(filename, options)
Return True
' Return false if the CSV export failed.
Catch
Return False
End Try
End Function
Call the PrintingSystem.ExportToCsv overloaded method to export a document. To specify export options, create the CsvExportOptions class instance and pass this instance to the invoked method, or use the ExportOptions.Csv property.
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using DevExpress.Utils;
// ...
public bool ExportDocument(PrintingSystem printingSystem, string filename) {
try {
// Specify CSV export options.
var options = new CsvExportOptions();
options.SkipEmptyColumns = false;
options.SkipEmptyRows = false;
options.EncodeExecutableContent = DefaultBoolean.True;
// Export a document to CSV.
printingSystem.ExportToCsv(filename, options);
return true;
}
// Return false if the CSV export failed.
catch {
return false;
}
}
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.Utils
' ...
Public Function ExportDocument(ByVal printingSystem As PrintingSystem, ByVal filename As String) As Boolean
Try
' Specify CSV export options.
Dim options = New CsvExportOptions()
options.SkipEmptyColumns = False
options.SkipEmptyRows = False
options.EncodeExecutableContent = DefaultBoolean.True
' Export a document to CSV.
printingSystem.ExportToCsv(filename, options)
Return True
' Return false if the CSV export failed.
Catch
Return False
End Try
End Function
Call the PrintingLink.ExportToCsv overloaded method to export a control. To specify export options, create the CsvExportOptions class instance and pass this instance to the invoked method.
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using DevExpress.Utils;
// ...
public bool ExportControl(IPrintable control, string filename) {
try {
PrintingSystem printingSystem = new PrintingSystem();
PrintableComponentLink link = new PrintableComponentLink();
printingSystem.Links.Add(link);
link.Component = control;
// Specify CSV export options.
var options = new CsvExportOptions();
options.SkipEmptyColumns = false;
options.SkipEmptyRows = false;
options.EncodeExecutableContent = DefaultBoolean.True;
// Export a document to CSV.
link.ExportToCsv(filename, options);
return true;
}
// Return false if the CSV export failed.
catch {
return false;
}
}
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.Utils
' ...
Public Function ExportControl(ByVal control As IPrintable, ByVal filename As String) As Boolean
Try
Dim printingSystem As New PrintingSystem()
Dim link As New PrintableComponentLink()
printingSystem.Links.Add(link)
link.Component = control
' Specify CSV export options.
Dim options = New CsvExportOptions()
options.SkipEmptyColumns = False
options.SkipEmptyRows = False
options.EncodeExecutableContent = DefaultBoolean.True
' Export a document to CSV.
link.ExportToCsv(filename, options)
Return True
' Return false if the CSV export failed.
Catch
Return False
End Try
End Function
Note
You cannot export a report that uses another report’s page. The exported file contains only one of the merged reports.
The following is suggested as an alternative solution:
Use the following properties to change the CSV export options:
CsvExportOptions.TextExportModeText mode exports formatted data (“Saturday, 1 January 2020”). Value mode exports values without formatting (“1/1/2020 12:00:00 AM”).CsvExportOptions.SkipEmptyColumns and CsvExportOptions.SkipEmptyRowsSpecify whether to include empty columns and/or rows in the exported file.EncodeExecutableContentSpecifies whether to encode potentially dangerous content in the exported file. This option is available at runtime only.
The report controls must not overlap to export to CSV correctly.
The Report Designer highlights overlapped controls. Users can hover over these controls to see what should be fixed. You can disable the DesignerOptions.ShowExportWarnings option to hide the highlighting.
To prevent possible security vulnerabilities, we automatically encode potentially dangerous content prior to exporting it to the CSV format. For example, formulas are encoded into simple data to prevent command execution when the exported file is opened in a different location.
This behavior is controlled by the following properties:
The controls are translated to text as follows:
XRLabelExported as a string.XRRichTextExported as a one-dimensional array of strings.XRPictureBoxThe control has no text representation.
User changes in Preview are not included in exported CSV files if the report uses the CachedReportSource component.
The export engine generates a tabular structure where columns and rows are used to position output elements as required by the visual report layout. This technical implementation may result in extra columns or rows, or even merged cells, which makes it hard to work with the documents for data analysis purposes. Please consider implementing data export features in your applications separately from Reporting. For the Document Viewer or Report Designer, you can customize the Export command and specify your own Export to CSV method.
This section contains several problematic scenarios with possible workarounds to help keep your reports compatible with data-centric exports.
Empty space between individual report controls may result in empty data rows or columns in the CSV file. The same effect occurs when the columns in the table header are not aligned correctly with those in the detail band.
You can use the following options to avoid empty cells in the resulting export file:
Tip
You may find the following section related to similar problems helpful when exporting to XLSX: Merged Cells And Extra Columns/Rows: Troubleshooting.
You can assign calculated values to the Value property instead of the Text property, and your dynamic values are included in CSV exports correctly. Alternatively, you can change the CsvExportOptions.TextExportMode to Text, to export all values as strings, formatted as they appear in the report.
See Also