New to Telerik UI for WinFormsStart a free 30-day trial

Export RadGridView in Docx document using RadWordsProcessing

Updated over 6 months ago

Environment

ProductVersionAuthor
RadGridView for WinForms2024.2.514Nadya Todorova

Description

RadGridView supports exporting its content to Excel, CSV, HTML, and PDF. For more information see Exporting Data. RadGridView doesn't support exporting to a .docx file out of the box. The following example describes how you can achieve such format conversion and export the grid to .docx document.

Solution

Use the GridViewSpreadExport to export the data to xlsx file. Then use the RadWordsProcessing library to import the exported file and then export it to .docx format. Please refer to the following code snippet:

Export RadGridView in Docx document

C#
private void radButton_Click(object sender, EventArgs e)
{
    GridViewSpreadExport spreadExporter = new GridViewSpreadExport(this.radGridView1);
    spreadExporter.ExportFormat = Telerik.WinControls.Export.SpreadExportFormat.Txt;
    SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
    spreadExporter.RunExport(@"..\..\exportedFile.xlsx", exportRenderer);
    Telerik.Windows.Documents.Flow.FormatProviders.Txt.TxtFormatProvider txtProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Txt.TxtFormatProvider();
    RadFlowDocument document;
    using (Stream input = File.OpenRead(@"..\..\exportedFile.xlsx"))
    {
        document = txtProvider.Import(input);
    }

    Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
    using (Stream output = File.OpenWrite(@"..\..\Sample.docx"))
    {
        provider.Export(document, output);
    }
}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support