XtraReport.ExportToCsv(String) Method
Exports a report to the specified file path in CSV format.
Namespace: DevExpress.XtraReports.UI
Assembly: DevExpress.XtraReports.v18.2.dll
Declaration
Parameters
Name | Type | Description |
---|---|---|
path | String | A String which specifies the file name (including the full path) for the created CSV file. |
Remarks
Once the document export has started, it will run until the resulting document is complete and cannot be interrupted or canceled in the process.
Use this method to export a report to a CSV file using its current CSV-specific export options. These options are represented by the CsvExportOptions object returned by the ExportOptions.Csv property of a report’s XtraReport.ExportOptions. This object provides the TextExportOptionsBase.Encoding, TextExportOptionsBase.Separator and other properties which are intended to specify parameters of the resulting CSV file.
If you want to ignore current export options of a report, and use your specific settings when a report is exported to CSV, you should use the overloaded XtraReport.ExportToCsv method with the options parameter.
Note
This method overwrites any files present at the specified path that have the same file name without any warnings.
Note
For more information on using the ExportToCsv method and limitations on export to CSV, please refer to the Export to CSV document.
Example
The following example demonstrates how to export a report to CSV format. To do this, the XtraReport.ExportToCsv method should be used. It also demonstrates what specific CsvExportOptions may be used when a report is exported to CSV.
For this example to work, you first need to add a new report (named XtraReport1) to your project, drop some report controls onto it, and then execute the code below (for instance, in the Button.Click event handler of any button on a form). Note also, that if you want the resulting file to be automatically opened in the default program which is used to open *.csv files in your system, you can call the StartProcess method, which is also shown in this example.
Tip
Online Example: How to export a report to CSV format
using System.Text;
using System.Diagnostics;
using System.Globalization;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...
private void ExportToCSV()
{
// A path to export a report.
string reportPath = "c:\\Test.csv";
// Create a report instance.
XtraReport1 report = new XtraReport1();
// Get its CSV export options.
CsvExportOptions csvOptions = report.ExportOptions.Csv;
// Set CSV-specific export options.
csvOptions.Encoding = Encoding.Unicode;
csvOptions.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator.ToString();
// Export the report to CSV.
report.ExportToCsv(reportPath);
// Show the result.
StartProcess(reportPath);
}
// Use this method if you want to automaically open
// the created CSV file in the default program.
public void StartProcess(string path)
{
Process process = new Process();
try
{
process.StartInfo.FileName = path;
process.Start();
process.WaitForInputIdle();
}
catch { }
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the ExportToCsv(String) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.