欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Word文件转换成HTML格式

程序员文章站 2022-04-09 09:12:54
...

首先引用:Microsoft.Office.Interop.Word.dll

若果 是.net 4.0以上版本添加引用,如果不是4.0以上版本可能没有这个,可以网上下载一个Microsoft.Office.Interop.Word.dll。

       // 将word文件中的数据读取为html语句
        public static string DocToHtml(object wordFileName)
        {
            //在此处放置用户代码以初始化页面 
            Word.Application word = new Word.Application();
            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[] { wordFileName, true, true });
            //转换格式,另存为 
            Type docType = doc.GetType();
            string wordSaveFileName = wordFileName.ToString();
            string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "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);
            //退出 Word 
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            return saveFileName.ToString();
        }


例如:   <asp:FileUpload ID="fu_NewsContent" runat="server" />

       if (fu_NewsContent.HasFile)
        {
                string fileName = fu_NewsContent.PostedFile.FileName;
                int extendNameIndex = fileName.LastIndexOf(".");
                string extendName = fileName.Substring(extendNameIndex);
                if (extendName == ".doc")
                {
                    string serverPath = Server.MapPath("~/UploadFiles/WordFiles/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
                    if (!Directory.Exists(serverPath))
                        Directory.CreateDirectory(serverPath);   //创建路径
                    DateTime now = DateTime.Now;
                    string fileTime = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString() + extendName;
                    fileName = serverPath + fileTime;
                    fu_NewsContent.PostedFile.SaveAs(fileName);   //保存word

                    string workHtml = CommonHelp.WordToHtml.DocToHtml(fileName);//转换
                    StreamReader fread = new StreamReader(workHtml, System.Text.Encoding.GetEncoding("gb2312"));
                    string ssRead = fread.ReadToEnd();
                    ssRead = ssRead.Replace("src=\"", "src=\"/UploadFiles/WordFiles/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
                    string News_Content = Encoding.Default.GetBytes(ssRead);
                    string News_FileUrl = "/UploadFiles/WordFiles/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + fileTime;
                    fread.Close();
                    fread.Dispose();
                }
                else               
                  return;  
         }