Asp.net2.0實現Word轉換Html

 前兩天在園子裏看到了BlackSoul 寫的asp.net將word轉換爲html保存 》的文章,覺得很實用,但是搭建項目時候缺不是那麼回事兒,中間有很多問題,比如說word組建的引用、web.config權限的設置,看到了很多人在網上也在問這個問題,我把問題一一解決,測試成功,現在把全部項目源碼文件分享給大家下載。
項目截圖:
 
   功能概述及注意事項:
該源碼主要是通過Asp.net2.0實現Word文檔上傳並自動轉換爲Html文件,原理是將word文檔上傳至服務器然後再轉存爲html格式文件,再解析html文件修改其頁面樣式和css。

wordTmp爲上傳是word暫存文件夾
html爲轉換後html保存文件夾

注意:請設置web.config中的<identity impersonate="true" userName="administrator" password="51aspx"/>帳號和密碼,否則會提示檢索 COM 類工廠中 CLSID 爲 {000209FF-0000-0000-C000-000000000046} 的組件時失敗,原因是出現以下錯誤: 80070005。

該用戶類型爲擁有user權限的用戶即可

  示例Word文件是些笑話,希望能博得大家多多笑容!

部分CS源碼:

  1    public partial class _Default : System.Web.UI.Page
  2    {
  3      //  public WordToHTML() { }
  4
  5        #region 上傳文件並轉換爲html wordToHtml(wordFilePath)
  6        /// 
  7        /// 上傳文件並轉存爲html
  8        /// 
  9        /// word文檔在客戶機的位置
 10        /// 上傳的html文件的地址

 11        public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
 12        {
 13            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
 14            Type wordType = word.GetType();
 15            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
 16
 17            // 打開文件
 18            Type docsType = docs.GetType();
 19
 20            //應當先把文件上傳至服務器然後再解析文件爲html
 21            string filePath = uploadWord(wordFilePath);
 22
 23            //判斷是否上傳文件成功
 24            if (filePath == "0")
 25                return "0";
 26            //判斷是否爲word文件
 27            if (filePath == "1")
 28                return "1";
 29
 30            object fileName = filePath;
 31
 32            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
 33            System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, truetrue });
 34
 35            // 轉換格式,另存爲html
 36            Type docType = doc.GetType();
 37
 38            string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
 39            System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
 40
 41            //被轉換的html文檔保存的位置
 42            string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
 43            object saveFileName = ConfigPath;
 44
 45            /*下面是Microsoft Word 9 Object Library的寫法,如果是10,可能寫成:
 46            * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
 47            * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
 48            * 其它格式:
 49            * wdFormatHTML
 50            * wdFormatDocument
 51            * wdFormatDOSText
 52            * wdFormatDOSTextLineBreaks
 53            * wdFormatEncodedText
 54            * wdFormatRTF
 55            * wdFormatTemplate
 56            * wdFormatText
 57            * wdFormatTextLineBreaks
 58            * wdFormatUnicodeText
 59            */

 60            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
 61            null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
 62
 63            // 退出 Word
 64            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
 65            //轉到新生成的頁面
 66            return ("/" + filename + ".html");
 67        }

 68        #endregion

 69
 70        public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
 71        {
 72            if (uploadFiles.PostedFile != null)
 73            {
 74                string fileName = uploadFiles.PostedFile.FileName;
 75                int extendNameIndex = fileName.LastIndexOf(".");
 76                string extendName = fileName.Substring(extendNameIndex);
 77                string newName = "";
 78                try
 79                {
 80                    //驗證是否爲word格式
 81                    if (extendName == ".doc")
 82                    {
 83
 84                        DateTime now = DateTime.Now;
 85                        newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
 86                        //上傳路徑 指當前上傳頁面的同一級的目錄下面的wordTmp路徑
 87                        uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
 88                    }

 89                    else
 90                    {
 91                        return "1";
 92                    }

 93                }

 94                catch
 95                {
 96                    return "0";
 97                }

 98                //return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
 99                return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
100            }

101
102            else
103            {
104                return "0";
105            }

106        }

107
108        protected void btnUpload_Click(object sender, EventArgs e)
109        {
110            try
111            {
112                //上傳
113                uploadWord(File1);
114                //轉換
115                wordToHtml(File1);
116            }

117            catch (Exception ex)
118            {
119                throw ex;
120            }

121            finally
122            {
123                Response.Write("恭喜,轉換成功!");
124            }

125        }

 
發佈了11 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章