Asp.net中將Word文件轉(zhuǎn)換成HTML的方法
本文所述為一個(gè)Asp.net實(shí)現(xiàn)將Word轉(zhuǎn)換為HTML的功能,其關(guān)鍵代碼如下:
//存放word文件的完整路徑 string wordPath = Server.MapPath("/word/test.doc"); //存放html文件的完整路徑 string htmlPath = Server.MapPath("/html/test.html"); //上傳word文件, 由于只是做示例,在這里不多做文件類型、大小、格式以及是否存在的判斷 FileUpload1.SaveAs(wordPath); #region 文件格式轉(zhuǎn)換 //請(qǐng)引用Microsoft.Office.Interop.Word ApplicationClass word = new ApplicationClass(); Type wordType = word.GetType(); Documents docs = word.Documents; //打開(kāi)文件 Type docsType = docs.GetType(); object fileName = wordPath; //"f:\\cc.doc"; Document doc =(Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, (object)docs, new Object[] { fileName, true, true}); //判斷與文件轉(zhuǎn)換相關(guān)的文件是否存在,存在則刪除。(這里,最好還判斷一下存放文件的目錄是否存在,不存在則創(chuàng)建) if(File.Exists(htmlPath)) { File.Delete(htmlPath); } //每一個(gè)html文件,有一個(gè)對(duì)應(yīng)的存放html相關(guān)元素的文件夾(html文件名.files) if(Directory.Exists(htmlPath.Replace(".html" ,".files"))) { Directory.Delete(htmlPath.Replace(".html", ".files"), true); }; //轉(zhuǎn)換格式,調(diào)用word的“另存為”方法 Type docType =doc.GetType(); object saveFileName = htmlPath; //"f:\\aaa.html"; docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatHTML }); //退出 Word wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null); #endregion
上述代碼,在.net framework4.0 中,可能會(huì)出一編譯錯(cuò)誤,如下所示:
無(wú)法嵌入互操作類型“……”,請(qǐng)改用適用的接口。
經(jīng)過(guò)查閱資料,找到解決方案如下:
選中項(xiàng)目中引入word的dll,鼠標(biāo)右鍵,選擇屬性,把“嵌入互操作類型”設(shè)置為False。
該實(shí)例完整代碼點(diǎn)擊此處本站下載。
- ASP.NET實(shí)現(xiàn)將word文檔轉(zhuǎn)換成pdf的方法
- asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
- asp.net實(shí)現(xiàn)word文檔在線預(yù)覽功能的方法
- asp.net中Word轉(zhuǎn)Html的辦法(不需要WORD組件)
- 從ASP.NET得到Microsoft Word文檔的代碼
- asp.net中調(diào)用Office來(lái)制作3D統(tǒng)計(jì)圖的實(shí)例代碼
- ASP.Net頁(yè)面生成餅圖實(shí)例
- asp.net檢查服務(wù)器上目錄或文件是否存在的方法
- asp.net實(shí)現(xiàn)將ppt文檔轉(zhuǎn)換成pdf的方法
相關(guān)文章
asp.net生成靜態(tài)頁(yè)并分頁(yè)+ubb
編程思路生成靜態(tài)頁(yè)其實(shí)只有內(nèi)容部分是需要分頁(yè)的,生成靜態(tài)頁(yè)并分頁(yè)的步驟是在textarea或者在線編輯器中,需要分頁(yè)的地方就插入一個(gè)[ page ]2008-10-10asp.net下gridview 批量刪除的實(shí)現(xiàn)方法
asp.net下gridview 批量刪除的實(shí)現(xiàn)方法...2007-11-11Cookies的各方面知識(shí)(基礎(chǔ)/高級(jí))深度了解
Cookies想必所有人都了解本文將圍繞Cookies基礎(chǔ)知識(shí)(什么是Cookies/Cookies如何傳遞/Cookies如何存儲(chǔ)/Cookies如何查看)Cookies高級(jí)知識(shí)/Cookie的限制等等方方面面深入了解,感興趣的朋友可以參考下,或許對(duì)你學(xué)習(xí)cookies有所幫助2013-02-02asp.net下URL網(wǎng)址重寫(xiě)成.html格式、RSS、OPML的知識(shí)總結(jié)
asp.net下URL網(wǎng)址重寫(xiě)成.html格式、RSS、OPML的知識(shí)總結(jié)...2007-09-09