MVC浏览器在线显示Word文档

 原文引自: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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值