Skip to main content

Export to DOCX

  • 10 minutes to read

Export from Preview / Document Viewer

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 drop-down list with export formats and select the DOCX format.

Specify export mode and options in the Export Options dialog, then click OK.

Export Modes

MS Word does not support the concept of hardcoded pages. There are two modes (Single File and Single File Page By Page) you can use to mimic the original report layout in the exported document. Use the Export Mode option to specify one of these modes.

Single File

In Single File (continuous) mode, a multi-page report is converted into a single document with the Table-Based layout. The report’s page headers and footers are displayed only once - at the beginning and end of the exported document.

To put content in native header and footer sections of the resulting DOCX file, place controls with the content in the top margin band or bottom margin band (not in the page footer or header).

Use the RasterizeImages property to specify whether to rasterize vector images, such as pictures, charts, or barcodes. You can also use the RasterizationResolution property to specify the image resolution.

Single File Page By Page

In Single File Page By Page mode, the export follows the WYSIWYG paradigm. The report’s page headers and footers, and top and bottom margins, appear on every page of the exported document as they appear in the report’s Print Preview. In this mode, a report is exported to a document with the Frame-Based layout. You can use the Table layout option to enable the Table-Based layout for this mode.

Use the RasterizeImages property to specify whether to rasterize vector images, such as pictures, charts, or barcodes. You can also use the RasterizationResolution property to specify the image resolution.

Document Layouts

Table-Based Layout

The Table-Based layout is the default layout for reports exported in Single File mode. You can also use the Table layout option to enable this layout for reports exported in Single File Page By Page mode. When you export a report to DOCX with the Table-Based layout, a table with merged cells is created to mimic the original layout of the report’s controls.

The following example demonstrates the Table-Based layout of a report exported to DOCX.

Troubleshooting

The following specifics apply to documents exported with the Table-Based layout:

  • If you edit content inside the table after the export, the table cells grow to fit the new content size. Thus, the resulting document can differ from the initial document in Print Preview. To avoid this effect, enable the Keep row height option. If the option is set to false (the default value), row heights are not fixed. If you add a new line of text to a cell, the line increases the cell’s row height.

    KeepRowHeight = false KeepRowHeight = true
    RtfExportOptions-KeepRowHeight-false RtfExportOptions-KeepRowHeight
  • If a report contains overlapped controls, you cannot export the report to DOCX with the Table-Based layout. To export a report with overlapped controls to DOCX, use the Frame-Based layout or try to change the control’s positions to avoid overlap.

    Overlapped controls are highlighted in the Report Designer. Move the mouse pointer over these controls to see a detailed export warning:

    ShowExportWarnings

    Use a report’s HasExportWarningControls collection to check if there are controls that have export warnings.

Frame-Based Layout

The Frame-Based layout is the default layout for reports exported in Single File Page By Page mode. For Single File mode, this layout is not available. When you export a report with the Frame-Based layout, each report element (such as a line, an image, or text produced by the report control) is created in its own independent vertical frame. Each frame is a text box whose coordinates in the initial report are transformed to coordinates of a DOCX document to preserve the initial report layout.

Limitations

  • Vertical alignment in a table cell is lost. To fix this problem, export using Table-Based Layout. To enable Table-Based layout, check the Table layout button in the DOCX Export Options dialog box. You can also do this in code as the following code snippet shows:

    using DevExpress.XtraPrinting;
    
    DocxExportOptions options = new DocxExportOptions();
    options.ExportMode = DocxExportMode.SingleFile;
    options.TableLayout = true;
    report.ExportToDocx("your_file_name", options);
    

Troubleshooting

  • Overlapped Controls.

    If a report has XRLabel controls above the XRShape control, you may try to enable the AllowFloatingPictures option. It allows the report to create full-fledged floating objects with negative z-indices instead of frames with pictures, so that they appear behind the main content.

Note

The resulting text boxes cannot grow. If you add text to a text box and the content does not fit the text box size, the content is truncated.

The following examples demonstrate the Frame-Based layout of a report exported to DOCX.

Export Options

The following table lists DOCX-specific options and indicates the export modes that support these options:

Option

Single File

(Continuous)

Single File Page-by-Page

(Table-Based layout)

Single File Page-by-Page

(Frame-Based layout)

Description

Export watermarks

Icon-yes

Icon-yes

Icon-yes

Specifies whether to include the report’s text and image watermarks in the DOCX file.

Page range

Icon-no

Icon-yes

Icon-yes

Specifies the range of pages to export to DOCX.

Table layout

Icon-no

Icon-yes

Icon-yes

Determines whether to use the table or frame layout in the exported DOCX file.

Keep row height

Icon-yes

Icon-yes

Icon-no

Specifies whether to use fixed table row heights in the exported document.

Empty first page header footer

Icon-yes

Icon-no

Icon-no

Defines whether to display the header and footer contents on the exported document’s first page.

Export page breaks

Icon-yes

Icon-no

Icon-no

Specifies whether to include page breaks in the exported DOCX file.

Floating pictures

Icon-no

Icon-no

Icon-yes

Specifies whether to embed floating pictures in the exported DOCX file.

Export in Code

Export a Report

Use the XtraReport.ExportToDocx overloaded method to export a report. To export a report asynchronously in a separate task, use the XtraReport.ExportToDocxAsync overloaded method instead. To specify export options, create a DocxExportOptions class instance and pass this instance to the invoked method.

using System;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...
XtraReport report = new XtraReport() {
    Name = "Report Example"
};

// docxExportFile specifies the exported file's name and path.
// The user's Downloads folder is used as the default path.
string docxExportFile =
    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    + @"\Downloads\" + report.Name + ".docx";

// Specify report options and export the report.
DocxExportOptions DocxExportOptions = new DocxExportOptions() {
    ExportMode = DocxExportMode.SingleFile;
};

report.ExportToDocx(docxExportFile, DocxExportOptions);

You can also use the XtraReport‘s ExportOptions.Docx property to specify export options.

report.ExportOptions.Docx.ExportMode = DocxExportMode.SingleFile;
report.ExportToDocx(docxExportFile);

Export a Document

Use the PrintingSystem.ExportToDocx overloaded method to export a document. To specify export options, create a DocxExportOptions class instance and pass this instance to the invoked method.

using System;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...
string filePath =
    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    + @"\Downloads\" + "Report1.prnx";

XtraReport report = new XtraReport();

// Load a document (PRNX) to the report.
report.PrintingSystem.LoadDocument(filePath);

// docxExportFile specifies the exported file's name and path.
// The user's Downloads folder is used as the default path.
string docxExportFile =
    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    + @"\Downloads\" + "Report1.docx";

// Specify export options and export the document.
DocxExportOptions DocxExportOptions = new DocxExportOptions();
DocxExportOptions.ExportMode = DocxExportMode.SingleFile;
report.PrintingSystem.ExportToDocx(docxExportFile, DocxExportOptions);

You can also use PrintingSystem‘s ExportOptions.Docx property to specify export options.

report.PrintingSystem.ExportOptions.Docx.ExportMode = DocxExportMode.SingleFile;
report.PrintingSystem.ExportToDocx(docxExportFile);

Export a Control

Use the PrintingLink.ExportToDocx overloaded method to export a control. To specify export options, create a DocxExportOptions 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() {
    // Customize the chart.
    // ...
};

// Create a printing link.
PrintingSystem printingSystem = new PrintingSystem();
PrintableComponentLink link = new PrintableComponentLink();
printingSystem.Links.Add(link);
link.Component = chart;

// docxExportFile specifies the exported file's name and path.
// The user's Downloads folder is used as the default path.
string docxExportFile =
    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    + @"\Downloads\" + chart.Name + ".docx";

// Specify export options and export the control.
DocxExportOptions DocxExportOptions = new DocxExportOptions();
DocxExportOptions.ExportMode = DocxExportMode.SingleFile;
link.ExportToDocx(docxExportFile, DocxExportOptions);

Limitations

Single File

  • You cannot export a report that uses another report’s page as a Single File. The exported file contains only one of the merged reports. As a workaround, use subreports to combine multiple reports into a single document or export all reports to separate files, and then join these files in a single file.

  • If a report uses a CachedReportSource, page breaks and changes made in Preview are not included in Single File exports.

Single File Page By Page

  • If the Table layout option is enabled:

    • The XRRichText control may be repeated if it is split between pages.
    • Controls whose content is split across several pages can be exported incorrectly. If your document contains such controls, use this mode with the Frame-Based layout (set Table layout to false).
  • The mode does not support controls with rotated text.

Common

The XRTableOfContents control is not exported as a DOCX-native table of contents. In Single File export mode, page numbers are not displayed.

Note

The DevExpress.RichEdit.v24.1.Export.dll library is required to export reports to DOCX or RTF format.

Post-Process DOCX Files

You can post-process the resulting DOCX files with the help of a dedicated DevExpress library: Word Processing Document API. This product helps you edit, merge, split, password-protect, and digitally sign DOCX files.

You may also use Word Processing Document API to generate rich text documents from scratch. If that code-only approach suits your requirements, you don’t need to construct a report in the designer at all.

Word Processing Document API works in desktop and web applications that target a variety of platforms (Windows Forms, WPF, ASP.NET Web Forms, ASP.NET MVC, ASP.NET Core, Blazor, MAUI) and operating systems (Windows, Linux, macOS).

Document generation/editing API is available in different DevExpress subscriptions. The following list briefly outlines what products you need to license to access required functionality. For complete information on DevExpress subscriptions and included functionality, please visit our Subscription Comparison page.

If you haven’t yet done so, download our fully-functional 30-day trial version to try out DevExpress controls and libraries in your projects:

Download: Free 30-Day Trial