Skip to main content

Export to CSV

  • 6 minutes to read

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.

Export to CSV

To export a report document to CSV format in code, use one of the following techniques:

Export a Report

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;
    }
}

Export a Document

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;
    }
}

Export a Control

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;
    }
}

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 subreports to combine multiple reports into a single document.
  • Export your reports to separate CSV files, and then join these files in a single file.

Export Options

Use the following properties to change the CSV export options:

CsvExportOptions.TextExportMode
Text 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.SkipEmptyRows
Specify whether to include empty columns and/or rows in the exported file.
EncodeExecutableContent
Specifies whether to encode potentially dangerous content in the exported file. This option is available at runtime only.

Overlapped Controls

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.

ShowExportWarnings

Security Considerations

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:

Limitations

Report Controls Are Exported As Text

The controls are translated to text as follows:

XRLabel
Exported as a string.
XRRichTextBox
Exported as a one-dimensional array of strings.
XRPictureBox
The control has no text representation.

CachedReportSource Component

User changes in Preview are not included in exported CSV files if the report uses the CachedReportSource component.

Troubleshooting

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 Rows/Columns

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.

Dynamically Assigned Text Values Are Not Exported

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