Back to Devexpress

XtraReport.ExportToCsv(String, CsvExportOptions) Method

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xtrareport-dot-exporttocsv-x28-system-dot-string-devexpress-dot-xtraprinting-dot-csvexportoptions-x29.md

latest5.3 KB
Original Source

XtraReport.ExportToCsv(String, CsvExportOptions) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Exports a report to the specified file in CSV format.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public void ExportToCsv(
    string path,
    CsvExportOptions options = null
)
vb
Public Sub ExportToCsv(
    path As String,
    options As CsvExportOptions = Nothing
)

Parameters

NameTypeDescription
pathString

The path to the exported CSV file.

|

Optional Parameters

NameTypeDefaultDescription
optionsCsvExportOptionsnull

The CSV export options. You can omit this parameter to use the current report export options.

|

Remarks

Note

Once the document export has started, it runs to completion and you cannot interrupt or cancel it.

This method exports a report to a file in CSV format with the specified CSV export options.

If you do not specify export options, the method uses the current report export options. To access the report export options, use the XtraReport.ExportOptions.Csv notation.

Important

This method overwrites files with the same name without confirmation.

Use the ExportToCsvAsync(String, CsvExportOptions, CancellationToken) method instead of ExportToCsv to export a report asynchronously in a separate task.

Example

This example demonstrates how to export a report to CSV format.

The project uses the XtraReport.ExportToCsv method with the CsvExportOptions object as a parameter.

csharp
using System.Text;
using System.Diagnostics;
using System.Globalization;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...

private void ExportToCSV()
{
    // Specify 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 automatically 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 { }
}
vb
Imports System.Text
Imports System.Diagnostics
Imports System.Globalization
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
' ...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
    ' Specify a path to export a report.
    Dim reportPath As String = "c:\\Test.csv"

    ' Create a report instance.
    Dim report As New XtraReport1()

    ' Get its CSV export options.
    Dim csvOptions As CsvExportOptions = 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)
End Sub

' Use this method if you want to automatically open
' the created CSV file in the default program.
Public Sub StartProcess(ByVal path As String)
    Dim process As New Process()
    Try
        process.StartInfo.FileName = path
        process.Start()
        process.WaitForInputIdle()
    Catch
    End Try
End Sub

See Also

Export Reports

Export to CSV

XtraReport Class

XtraReport Members

DevExpress.XtraReports.UI Namespace