Export HTML content into Docx

Thread is closed for posting
1 posts, 0 answers
  1. 0FB9D9BC-DE82-4A54-A0D4-6034D3C617C0
    0FB9D9BC-DE82-4A54-A0D4-6034D3C617C0 avatar
    631 posts
    Member since:
    Apr 2022

    Posted 27 Dec 2017 Link to this post

    Requirements

    Telerik Product and Version

    3.5+

    Supported Browsers and Platforms

    All browsers supported by Telerik UI for ASP .NET AJAX

    Components/Widgets used (JS frameworks, etc.)

    VB

    PROJECT DESCRIPTION 

    There are cases when the data bound to RadGrid contains html tags, which is rendered as expected on the page.

    Example datatable with data containing Html tags.


    Data renderd on the page


    There would be no issues if the exporting was done into Html based Word document (.doc). All it takes is to define Html for the export format (Export Settings -> Word Format -> Html.)
    <ExportSettings>
        <Word Format="Html" />
    </ExportSettings>

    GridExport.doc


     In our case, however, XML Based Word Document (.docx) is the required format. Changing the Word Format to Docx, in the exported file, Html tags will be returned as string.

    GridExport.docx



    SOLUTION
     
    Export the grid using HTML for Word Format, convert the Html to Docx using our Document Processing Libraries and then return the result in the Page response for download.

    Set the Word format to Html

    <ExportSettings>
        <Word Format="Html" />
    </ExportSettings>

    Covnert the output to Docx using the GridExporting event of RadGrid

    Protected Sub RadGrid1_GridExporting(sender As Object, e As GridExportingArgs)
        If (e.ExportType = ExportType.Word) Then
            '' Create a RadFlowDocument object
            Dim document As RadFlowDocument = New RadFlowDocument()
     
            '' Importing the HTML output from the grid into the htmlProvider
            Dim htmlProvider As HtmlFormatProvider = New HtmlFormatProvider()
            document = htmlProvider.Import(e.ExportOutput)
     
            Dim data As Byte() = Nothing
     
            '' Changing the page orientation of all sections in a document
            For Each section As Section In document.EnumerateChildrenOfType(Of Section)()
                section.Rotate(PageOrientation.Landscape)
            Next
     
            'Dim iProvider As IFormatProvider(Of RadFlowDocument) = docxProvider
     
            '' export the final document (docx) into the MemoryStream
            Using ms As New MemoryStream()
                Dim docxProvider As DocxFormatProvider = New DocxFormatProvider()
                docxProvider.Export(document, ms)
                data = ms.ToArray() '' get the byte data of the document
            End Using
     
            '' send the data in the response for download
            Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            Response.Headers.Remove("Content-Disposition")
            Response.AppendHeader("Content-Disposition", "attachment; filename=" & RadGrid1.ExportSettings.FileName & ".docx")
            Response.BinaryWrite(data)
            Response.End()
        End If
    End Sub

    GridExport.docx


    One more thing to adjust in here. Note, the cell borders are not present in the picture above. To overcome that, apply the desired style to the cells inside the PreRender event of RadGrid.
    Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
        If RadGrid1.IsExporting Then
            '' Setting borders for the Header Cells
            For Each headerCell As TableHeaderCell In RadGrid1.MasterTableView.GetItems(GridItemType.Header)(0).Cells
                headerCell.Style("border") = "1px solid"
            Next
            '' Setting borders for the data cells
            For Each dataItem As GridDataItem In RadGrid1.Items
                For Each cell As GridTableCell In dataItem.Cells
                    cell.Style("border") = "1px solid"
                Next
            Next
        End If
    End Sub

    Final results: GridExport.docx
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.