Category / Section
Is it possible to export a Word document to a string of HTML without saving as file to disk?
1 min read
Yes, it is possible to export a Word document to a string of HTML without saving as file to disk.
Please refer the below UG link to know more about Word to HTML conversion.
The following code example shows how to save a Word document template as string of HTML without saving as file.
C#
// Loads an existing Word document. WordDocument wordDocument = new WordDocument(filePath); // Creates new MemoryStream to save Word document. MemoryStream stream = new MemoryStream(); // Saves and closes the WordDocument instance. wordDocument.Save(stream, FormatType.Html); wordDocument.Close();stream.Position = 0; // Converts stream to string StreamReader reader = new StreamReader(stream); string htmlString = reader.ReadToEnd(); stream.Dispose(); reader.Dispose(); // Displays the resultant HTML string in MessageBox. MessageBox.Show(htmlString);
VB
' Loads an existing Word document. Dim wordDocument As WordDocument = New WordDocument(filePath) ' Creates New MemoryStream to save Word document. Dim stream As MemoryStream = New MemoryStream() ' Saves And closes the WordDocument instance. wordDocument.Save(stream, FormatType.Html) wordDocument.Close() stream.Position = 0 ' Convert stream to string Dim reader As StreamReader = New StreamReader(stream) Dim HtmlString As String = reader.ReadToEnd() stream.Dispose() reader.Dispose() ' Displays the resultant HTML string in MessageBox. MessageBox.Show(htmlString)
The output window look like as follows.
The following sample with above codes demonstrates on how to save a Word document template as string of HTML without saving as file using Essential DocIO.