Export to Text

  • 6 minutes to read

This document describes how to export a report document to text 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 do this, expand the Export Document drop-down list and select Text File. Specify export options in the invoked dialog, and click OK.

To export a report document to text format in code, use one of the following approaches:

  • Export a Report

    Call an XtraReport.ExportToText overloaded method to export a report. To export a report asynchronously in a separate task, call an XtraReport.ExportToTextAsync overloaded method instead. To specify export options, create a TextExportOptions class instance and pass this instance to the invoked method, or use XtraReport's ExportOptions.Text property.

    using System;
    using DevExpress.XtraPrinting;
    using DevExpress.XtraReports.UI;
    // ...
    XtraReport report = new XtraReport() {
        Name = "HelloWorld",
        Bands = {
            new DetailBand(){
                Controls={
                    new XRLabel(){
                        Text="Hello World!"
                    }
                }
            }
        }
    };
    // textExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
    string textExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + report.Name + ".txt";
    // Specify export options to export the report.
    // Uncomment the lines below to specify export options in a TextExportOptions class instance.
    // TextExportOptions textExportOptions = new TextExportOptions();
    // textExportOptions.ExportMode = TextExportMode.SingleFile;
    // report.ExportToText(textExportFile, textExportOptions);
    
    // Comment the lines below if you specified export options in a TextExportOptions class instance above.
    report.ExportOptions.Text.ExportMode = TextExportMode.SingleFile;
    report.ExportToText(textExportFile);
    
  • Export a Document

    Call a PrintingSystem.ExportToText overloaded method to export a document. To specify export options, create a TextExportOptions class instance and pass this instance to the invoked method, or use PrintingSystem's ExportOptions.Text property.

    using System;
    using DevExpress.XtraPrinting;
    using DevExpress.XtraReports.UI;
    // ...
    XtraReport report = new XtraReport() {
        Name = "HelloWorld",
        Bands = {
            new DetailBand(){
                Controls={
                    new XRLabel(){
                        Text="Hello World!"
                    }
                }
            }
        }
    };
    // Create a document from the report.
    report.CreateDocument();
    // textExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
    string textExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + report.Name + ".txt";
    // Specify export options and export the document.
    // Uncomment the lines below to specify export options in a TextExportOptions class instance.
    // TextExportOptions textExportOptions = new TextExportOptions();
    // textExportOptions.ExportMode = TextExportMode.SingleFile;
    // report.PrintingSystem.ExportToText(textExportFile, textExportOptions);
    
    // Comment the lines below if you specified export options in a TextExportOptions class instance above.
    report.PrintingSystem.ExportOptions.Text.ExportMode = TextExportMode.SingleFile;
    report.PrintingSystem.ExportToText(textExportFile);
    
  • Export a Control

    Call a PrintingLink.ExportToText overloaded method to export a control. To specify export options, create a TextExportOptions class instance and pass this instance to the invoked method.

    using System;
    using DevExpress.XtraPrinting;
    using DevExpress.XtraReports.UI;
    // ...
    DevExpress.XtraCharts.ChartControl chart = new DevExpress.XtraCharts.ChartControl() {
        Name = "IncomeByQuarter",
        Series = {
            new DevExpress.XtraCharts.Series("2019", DevExpress.XtraCharts.ViewType.Bar)
        }
    };
    // Create a printing link.
    PrintingSystem printingSystem = new PrintingSystem();
    PrintableComponentLink link = new PrintableComponentLink();
    printingSystem.Links.Add(link);
    link.Component = chart;
    // textExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
    string textExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + chart.Name + ".txt";
    // Specify export options and export the control.
    TextExportOptions textExportOptions = new TextExportOptions();
    textExportOptions.ExportMode = TextExportMode.SingleFile;
    link.ExportToText(textExportFile, textExportOptions);
    

The report is expoted into one file that contains one page (the Single File mode). The report's page headers, footers, top and bottom margins appear only once, at the beginning and end of the resulting page.

Use the TextExportOptions.TextExportMode property to specify how to export text data from controls:

  • Set this property to Text to export data fields as strings. The formatting is embedded in these strings.
  • Set this property to Value to export data field values.

To ensure that report controls are exported correctly, they should not intersect. Otherwise, the file layout can be corrupted.

Overlapped controls are highlighted in reports. Users can move the mouse pointer over these controls to see what needs to be fixed.

ShowExportWarnings

Use a report's HasExportWarningControls collection to check if there are controls that have export warnings.
Disable a report's DesignerOptions.ShowExportWarnings property to remove highlights on overlapped controls.

You cannot export reports merged page by page in the Single File export mode. As a workaround:

  • Use subreports to merge multiple reports into a single document.
  • Export all reports to separate files and join these files in a single file.

When you export to text, the XRControl class's GetTextView protected method is invoked. This method is overridden in report controls and returns a 2-dimensional array of strings used for the text representation of a control. For instance, the XRLabel is represented as a string, the XRRichTextBox as a 1-dimensional array of strings, the XRTable as a 2-dimensional array of strings, and the XRPictureBox cannot be represented as a string.

If a report uses a CachedReportSource, changes made in Preview are not included in the exported file.