Skip to main content

Export to Text

  • 3 minutes to read

This document details the exporting of a document to Text.

The options that can be specified for a document exported to a TXT file are stored in the TextExportOptions class, and can be accessed via a report’s ExportOptions.Text property.

Among these options, the TextExportOptionsBase.TextExportMode property determines whether to use the formatting of data fields in the bound dataset for the cells in the exported Text document. Note that if this property is set to Text, all data fields are exported to the TXT file as strings with the corresponding formatting embedded into those strings.

Note

Note that only the report controls that don’t intersect with each other can be correctly exported to Text. In other cases, the resultant TXT file may have a completely broken layout.

To make sure that your report layout will be preserved in a Text 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).

ShowExportWarnings

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 TXT Format

The following example demonstrates how to export a report to text:

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

namespace ExportToTextCS {
    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.txt";

            // Create a report instance.
            XtraReport1 report = new XtraReport1();

            // Get its Text export options.
            TextExportOptions txtOptions = report.ExportOptions.Text;

            // Set Text-specific export options.
            txtOptions.Encoding = Encoding.Unicode;
            txtOptions.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator.ToString();

            // Export the report to Text.
            report.ExportToText(reportPath);

            // Show the result.
            StartProcess(reportPath);
        }

        // Use this method if you want to automaically open
        // the created Text file in the default program.
        public void StartProcess(string path) {
            Process process = new Process();
            try {
                process.StartInfo.FileName = path;
                process.Start();
                process.WaitForInputIdle();
            }
            catch { }
        }
    }
}