Back to Devexpress

Export to CSV

windowsforms-7319-controls-and-libraries-printing-exporting-concepts-exporting-export-to-csv.md

latest5.9 KB
Original Source

Export to CSV

  • Nov 15, 2018
  • 4 minutes to read

This document details exporting a document to CSV (comma-separated values) format.

The options which can be specified for a document exported to a CSV file are stored in the CsvExportOptions class, and can be accessed via a report’s ExportOptions.Csv property.

Note

Only the report controls that do not intersect with each other can be correctly exported to CSV. In other cases, the resulting CSV file may have a completely broken layout.

To make sure that your report layout will be preserved in a CSV format, enable the report’s DesignerOptions.ShowExportWarnings property at design time, and check to ensure there are no exclamation marks shown for intersecting controls (colored in red ).

When exporting to a CSV file, XtraReports uses the protected GetTextView method of the XRControl class, which is overridden in the appropriate report control. This method returns a two-dimensional string array used for the text representation of a control. For instance, the XRLabel is represented as a simple string, the XRRichText as a one-dimensional array of strings, the XRTable as a two-dimensional array of strings, and the XRPictureBox cannot be represented as a string.

Note

Composite report documents created from multiple merged documents cannot be exported to file formats that support a continuous (table-like) layout (such as TXT or CSV).

As a workaround, use subreports to combine multiple XtraReport to a single document. Alternatively, export all your reports to CSV and TXT files separately and then join all the exported data to a single file.

Example: How to Export a Report to CSV Format

The following example demonstrates how to export a report to CSV format:

csharp
using System;
using System.Windows.Forms;

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

namespace ExportToCsvCS {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            // 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 { }
        }
    }
}
vb
Imports System.Text
Imports System.Diagnostics
Imports System.Globalization
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
' ...

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 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 automaically 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
End Class