這篇文章主要講解了“Asp.Net在線預覽Word文檔問題怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Asp.Net在線預覽Word文檔問題怎么解決”吧!
Asp.Net不帶Core,.NET Framework 4.0,部署在Windows平臺上。
Word文檔轉換Html,再預覽Html。Word文檔轉Html先引用Office的DLL,在COM里面,注意:電腦需要安裝Office哦。


又注意:請在DLL屬性里面將嵌入互操作類型改為False

轉換過程一個方法搞定:
using Microsoft.Office.Interop.Word;
public static string WordToHtml(string path)
{
string root = AppDomain.CurrentDomain.BaseDirectory;
var htmlName = $"{Guid.NewGuid().ToString("N")}.html";
var htmlPath = root + $"Resource/Temporary/";
if (!Directory.Exists(htmlPath))
{
Directory.CreateDirectory(htmlPath);
}
ApplicationClass word = new ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
Type docType = doc.GetType();
docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { (htmlPath + htmlName), WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", BindingFlags.InvokeMethod, null, doc, null);
wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);
return htmlName;
}上一步Word轉Html的方法已經準備就緒,我們再準備這樣一個Word文檔。

簡單寫一下邏輯:


是不是特別簡單,我們再看看成品效果。


這種方案局限性比較大,部署平臺必須安裝Office,剛好客戶能滿足。
感謝各位的閱讀,以上就是“Asp.Net在線預覽Word文檔問題怎么解決”的內容了,經過本文的學習后,相信大家對Asp.Net在線預覽Word文檔問題怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。