原文引自:https://www.aspsnippets.com/Articles/Display-Word-document-on-web-page-in-ASP.Net.aspx
将word文档转换为html文件后,在网页中显示
Imports Microsoft.Office.Interop.Word
Dim documentFormat As Object = 8
Dim randomName As String = DateTime.Now.Ticks.ToString
Dim htmlFilePath As Object = Server.MapPath("~/Temp/") & randomName + ".htm"
Dim directoryPath As String = Server.MapPath("~/Temp/") & randomName + "_files"
'If Directory not present, create it.
If Not IO.Directory.Exists(Server.MapPath("~/Temp/")) Then
IO.Directory.CreateDirectory(Server.MapPath("~/Temp/"))
End If
'Open the word document in background.
Dim applicationclass As _Application = New Application
applicationclass.Documents.Open(fileSavePath)
applicationclass.Visible = False
Dim document As Document = applicationclass.ActiveDocument
'Save the word document as HTML file.
document.SaveAs(htmlFilePath, documentFormat)
'Close the word document.
document.Close()
'Read the saved Html File.
Dim wordHTML As String = System.IO.File.ReadAllText(htmlFilePath.ToString, Encoding.Default)
'Loop and replace the Image Path.
For Each match As Match In Regex.Matches(wordHTML, "<v:imagedata.+?src=[""'](.+?)[""'].*?>", RegexOptions.IgnoreCase)
wordHTML = Regex.Replace(wordHTML, match.Groups(1).Value, ("Temp/" + match.Groups(1).Value))
Next
ViewBag.tempHtml = MvcHtmlString.Create(If(wordHTML, String.Empty))
原版解决方案对中文显示乱码,需要修改如下解码设置:
Dim wordHTML As String = System.IO.File.ReadAllText(htmlFilePath.ToString, Encoding.Default)