Requirements
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.)
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
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.