日常生活中,我們總是在Word中進行文字的編輯,它不僅能夠保存Text文本,還可以保存文本的格式等等。那麼如果我要將一Word文檔上的內容展示在網頁上,該怎麼做呢?這裡我提供了一個小工具,你可以將Word轉換為Html,需要顯示的話,可以直接訪問該Html,廢話不多說,下面看代碼。 頁面代碼: [html] <span style="font-size:18px;"><div> <input id="File1" type="file" runat="server"/> <asp:Button ID="btnConvert" runat="server" Text="轉換" OnClick="btnConvert_Click" /> </div></span> C#代碼: [csharp] <span style="font-size:18px;">using System; using System.Data; using System.Configuration; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 將word轉換為Html /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnConvert_Click(object sender, EventArgs e) { try { //上傳 //uploadWord(File1); //轉換 wordToHtml(File1); } catch (Exception ex) { throw ex; } finally { Response.Write("恭喜,轉換成功!"); } } //上傳文件並轉換為html wordToHtml(wordFilePath) ///<summary> ///上傳文件並轉存為html ///</summary> ///<param name="wordFilePath">word文檔在客戶機的位置</param> ///<returns>上傳的html文件的地址</returns> public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath) { Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); Type wordType = word.GetType(); Microsoft.Office.Interop.Word.Documents docs = word.Documents; // 打開文件 Type docsType = docs.GetType(); //應當先把文件上傳至服務器然後再解析文件為html string filePath = uploadWord(wordFilePath); //判斷是否上傳文件成功 if (filePath == "0") return "0"; //判斷是否為word文件 if (filePath == "1") return "1"; object fileName = filePath; Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true }); // 轉換格式,另存為html Type docType = doc.GetType(); string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString(); // 判斷指定目錄下是否存在文件夾,如果不存在,則創建 if (!Directory.Exists(Server.MapPath("~\\html"))) { // 創建up文件夾 Directory.CreateDirectory(Server.MapPath("~\\html")); } //被轉換的html文檔保存的位置 string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html"); object saveFileName = ConfigPath; /*下面是Microsoft Word 9 Object Library的寫法,如果是10,可能寫成: * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML}); * 其它格式: * wdFormatHTML * wdFormatDocument * wdFormatDOSText * wdFormatDOSTextLineBreaks * wdFormatEncodedText * wdFormatRTF * wdFormatTemplate * wdFormatText * wdFormatTextLineBreaks * wdFormatUnicodeText */ docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); //關閉文檔 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { null, null, null }); // 退出 Word wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); //轉到新生成的頁面 return ("/" + filename + ".html"); } public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles) { if (uploadFiles.PostedFile != null) { string fileName = uploadFiles.PostedFile.FileName; int extendNameIndex = fileName.LastIndexOf("."); string extendName = fileName.Substring(extendNameIndex); string newName = ""; try { //驗證是否為word格式 if (extendName == ".doc" || extendName == ".docx") { DateTime now = DateTime.Now; newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString(); // 判斷指定目錄下是否存在文件夾,如果不存在,則創建 if (!Directory.Exists(Server.MapPath("~\\wordTmp"))) { // 創建up文件夾 Directory.CreateDirectory(Server.MapPath("~\\wordTmp")); } //上傳路徑 指當前上傳頁面的同一級的目錄下面的wordTmp路徑 uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName)); } else { return "1"; } } catch { return "0"; } www.2cto.com //return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName; return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName); } else { return "0"; } }</span> 效果圖: 轉換後的Html文件 這樣就可以簡單的在Html中展示word文檔中的內容,而不需要在自己進行編輯了。當然,如果有需要的話,可以將轉換的Html的路徑存入數據庫,根據不同的條件直接進行訪問。