Export to CSV
- 5 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.
To export a report document to CSV format in code, use one of the following approaches:
Export a Report
Call an XtraReport.ExportToCsv overloaded method to export a report. To specify export options, create a CsvExportOptions class instance and pass this instance to the invoked method, or use the XtraReport‘s 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 a PrintingSystem.ExportToCsv overloaded method to export a document. To specify export options, create a CsvExportOptions class instance and pass this instance to the invoked method, or use PrintingSystem‘s 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 a PrintingLink.ExportToCsv overloaded method to export a control. To specify export options, create a 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; } }
Tip
Code example: How to export a report to CSV format.
To export a control to CSV, a report uses the control’s protected GetTextView method that returns a two-dimensional string array as the control’s text representation:
- An XRLabel appears as a string.
- An XRRichTextBox appears as a one-dimensional array of strings.
- An XRPictureBox cannot be represented as a string.
Note
You cannot export reports that use other reports’ pages. The exported file contains only one of the merged reports.
As a workaround:
- Use subreports to combine multiple reports into a single document.
- Export all your reports to separate CSV files, then join these files in a single file.
Export Options
Use the following properties to change the CSV export options:
CsvExportOptions.TextExportMode
Specifies how to export text data from controls.
- Set this property to Text to export all data fields as strings. The formatting is embedded in these strings.
- Set this property to Value to export the values only.
CsvExportOptions.SkipEmptyColumns and CsvExportOptions.SkipEmptyRows
These properties specify whether to include empty columns and/or rows in the exported file.
-
Specifies whether to encode the potentially dangerous content in the exported file. This option is available at runtime only.
Overlapped Controls
Report controls should not intersect to be correctly exported to CSV. Otherwise, the file layout can be corrupted.
Report Designers highlight overlapped controls. Users can hover the mouse pointer over these controls to see what they need to fix.