怎么在asp.net項(xiàng)目中實(shí)現(xiàn)一個(gè)在線預(yù)覽word文檔功能?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
1、首先引入com組件中office庫(kù),然后在程序集擴(kuò)展中引入word的dll
2、將Microsoft.Office.Interop.Word的嵌入互操作類型設(shè)置為 false,如圖
3、主要代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
namespace Wolfy.OfficePreview
{
public class Office2HtmlHelper
{
///
/// Word轉(zhuǎn)成Html
///
/// 要轉(zhuǎn)換的文檔的路徑
/// 轉(zhuǎn)換成html的保存路徑
/// 轉(zhuǎn)換成html的文件名字
public static void Word2Html(string path, string savePath, string wordFileName)
{
Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
Type docsType = docs.GetType();
Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
Type docType = doc.GetType();
string strSaveFileName = savePath + wordFileName + ".html";
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
}
}
}
調(diào)用:
復(fù)制代碼 代碼如下:
Office2HtmlHelper.Word2Html(MapPath("/Doc/分析某網(wǎng)站的SEO策略(外鏈篇).doc"), MapPath("/Html/"), "分析某網(wǎng)站的SEO策略(外鏈篇)");
看完上述內(nèi)容,你們掌握怎么在asp.net項(xiàng)目中實(shí)現(xiàn)一個(gè)在線預(yù)覽word文檔功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!