Esempio n. 1
0
        /// <summary>
        /// word转html
        /// </summary>
        /// <param name="wordFileName">word文档访问路径(服务器绝对路径)</param>
        /// <param name="htmlWord">html保存目录(服务器绝对路径)</param>
        /// <returns>true/false</returns>
        private bool WordToHtml(string wordFileName, string htmlWord)
        {
            bool isConvert = false;

            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();

            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            try
            {
                //打开文件
                Type docsType = docs.GetType();
                Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.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  = htmlWord + "\\" + Path.GetFileNameWithoutExtension(wordSaveFileName) + ".html";
                object saveFileName     = (object)strSaveFileName;
                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, null);
                isConvert = true;
            }
            catch (Exception ex)
            {
                isConvert = false;
            }
            //退出Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            return(isConvert);
        }
Esempio n. 2
0
        /// <summary>
        /// Word转成Html
        /// </summary>
        /// <param name="filePath">word文档路径</param>
        /// <param name="htmlPath">保存的html路径</param>
        /// <returns>是否成功</returns>
        public static bool WordToHtml(string filePath, string htmlPath = null)
        {
            var  flg      = false;
            var  word     = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();

            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)filePath, true, true });
            Type docType = doc.GetType();

            string strSaveFileName = htmlPath ?? System.IO.Path.ChangeExtension(filePath, "html");

            object saveFileName = (object)strSaveFileName;

            try
            {
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
                flg = true;
            }
            catch (Exception ex) { throw ex; }
            try
            {
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch { }
            return(flg);
        }
Esempio n. 3
0
 private WordApplication()
 {
     this.m_WordApp = new Microsoft.Office.Interop.Word.Application();
     this.m_WordApp.Visible = false;
     this.m_Documents = this.m_WordApp.Documents;
     this.m_Document = this.m_Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oTrue);
     this.m_WordSuggestionList = new List<string>();
 }
Esempio n. 4
0
 private WordApplication()
 {
     this.m_WordApp            = new Microsoft.Office.Interop.Word.Application();
     this.m_WordApp.Visible    = false;
     this.m_Documents          = this.m_WordApp.Documents;
     this.m_Document           = this.m_Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oTrue);
     this.m_WordSuggestionList = new List <string>();
 }
Esempio n. 5
0
 private void allocWord()
 {
     if (avaWord)
     {
         docApp  = new Microsoft.Office.Interop.Word.Application();
         docDocs = docApp.Documents;
     }
 }
Esempio n. 6
0
        public static void PrintWord(object FileName, string ConnectionString, string coTitle, string sFontName, string Lang, string where,
                                     string where1, string where2, string where3, string where4, string where5, string where6, string where7, string where8,
                                     string where9, string where10, string User)
        {
            object oMissing = Missing.Value;

            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Documents   oDocs = oWord.Documents;
            Microsoft.Office.Interop.Word._Document   oDoc  = null;

            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            try
            {
                oDoc = oDocs.Open(ref FileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                  ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                oWord.Visible = true;

                //Cap nhat properties
                SetProperty(oDoc, "ConnectionString", ConnectionString);
                SetProperty(oDoc, "CoTitle", coTitle);
                SetProperty(oDoc, "sFontName", sFontName);
                SetProperty(oDoc, "Lang", Lang);
                SetProperty(oDoc, "where", where);
                SetProperty(oDoc, "where1", where1);
                SetProperty(oDoc, "where2", where2);
                SetProperty(oDoc, "where3", where3);
                SetProperty(oDoc, "where4", where4);
                SetProperty(oDoc, "where5", where5);
                SetProperty(oDoc, "where6", where6);
                SetProperty(oDoc, "where7", where7);
                SetProperty(oDoc, "where8", where8);
                SetProperty(oDoc, "where9", where9);
                SetProperty(oDoc, "where10", where10);
                SetProperty(oDoc, "User", User);

                try
                {
                    RunMacro(oWord, new object[] { "CreatReport" });
                }
                catch {}
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error!");
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
                GC.Collect();
            }
        }
Esempio n. 7
0
 private void freeWord()
 {
     if (avaWord)
     {
         if (docDocs != null)
         {
             docDocs = null;
         }
         ((Microsoft.Office.Interop.Word._Application)docApp).Quit();
         ;
     }
 }
        /// <summary>
        /// Word 转换成 HTML 文件
        /// </summary>
        /// <param name="wordPath">Word 文档路径</param>
        /// <param name="htmlPath">Html 文件路径</param>
        public static void WordToHtmlFile(string wordPath, string htmlPath)
        {
            if (string.IsNullOrEmpty(wordPath))
            {
                throw new Exception(WordPathNullException);
            }

            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = applicationClass.GetType();

            Microsoft.Office.Interop.Word.Documents documents = applicationClass.Documents;

            // 打开文件
            Type documentsType = documents.GetType();

            Microsoft.Office.Interop.Word.Document document = (Microsoft.Office.Interop.Word.Document)documentsType.InvokeMember("Open",
                                                                                                                                 System.Reflection.BindingFlags.InvokeMethod, null, documents, new Object[] { wordPath, true, true });

            // 转换格式,另存为html
            Type documentType = document.GetType();

            string directoryPath     = Path.GetDirectoryName(htmlPath);
            string fileDirectoryPath = Path.Combine(directoryPath, Path.GetFileNameWithoutExtension(htmlPath));

            /*下面是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
             */
            documentType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                                      null, document, new object[] { htmlPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

            //关闭文档
            documentType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
                                      null, document, new object[] { null, null, null });

            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, applicationClass, null);

            //转化HTML页面统一编码格式
            TransformHTMLEncoding(htmlPath);
        }
Esempio n. 9
0
        public override void Convert(string src, string dest, out string msg)
        {
            msg = null;
            Object nothing = System.Reflection.Missing.Value;

            try
            {
                if (string.IsNullOrEmpty(src))
                {
                    msg = "源文件不能为空";
                    return;
                }
                var contentType = MimeTypes.GetContentType(src);
                if (contentType != "application/msword")
                {
                    msg = "源文件应为word文件";
                    return;
                }
                if (string.IsNullOrEmpty(dest))
                {
                    msg = "目标路径不能为空";
                    return;
                }
                if (!File.Exists(src))
                {
                    msg = "文件不存在";
                    return;
                }

                if (IsPasswordProtected(src))
                {
                    msg = "存在密码";
                    return;
                }

                app  = new Microsoft.Office.Interop.Word.Application();
                docs = app.Documents;
                doc  = docs.Open(src, false, true, false, nothing, nothing, true, nothing, nothing, nothing, nothing, false, false, nothing, true, nothing);
                doc.ExportAsFixedFormat(dest, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, false, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, false, false, Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, false, false, nothing);
            }
            catch (Exception e)
            {
                release();
                throw new ConvertException(e.Message);
            }
            release();
        }
Esempio n. 10
0
        /// <summary>
        /// Word转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void Word2Html(string path, string savePath, string wordFileName)
        {
            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();

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.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, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
Esempio n. 11
0
    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();
        //被转换的html文档保存的位置
        string ConfigPath   = HttpContext.Current.Server.MapPath("~/keyfile/a.html");
        object saveFileName = ConfigPath;


        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                             null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });
        //关闭文档
        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");
    }
Esempio n. 12
0
        /// <summary>
        /// 转换word为pdf
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="savefilename"></param>
        public static void ConvertWordPDF2(object filename, object savefilename)
        {
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

            Type wordType = wordApp.GetType();

            Microsoft.Office.Interop.Word.Documents docs = wordApp.Documents;

            Type docsType = docs.GetType();

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                                                                       System.Reflection.BindingFlags.InvokeMethod, null, (object)docs, new Object[] { filename, true, true });

            Type docType = doc.GetType();

            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF });

            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);

            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, wordApp, null);
        }
Esempio n. 13
0
        //上传文件并转换为html wordToHtml(wordFilePath)
        ///<summary>
        ///上传文件并转存为html
        ///</summary>
        ///<param name="wordFilePath">word文档在客户机的位置</param>
        ///<returns>上传的html文件的地址</returns>
        public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
        {
            Alert.Show("hello");
            //txt_TitleName.Text = "12";
            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();

            // 判断指定目录下是否存在文件夹,如果不存在,则创建
            string abc = Server.MapPath("~\\html");

            Alert.Show("html文件路径" + abc);
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert(" + abc + ")", true);
            if (!Directory.Exists(Server.MapPath("~\\html")))
            {
                // 创建up文件夹
                Directory.CreateDirectory(Server.MapPath("~\\html"));
            }

            //被转换的html文档保存的位置
            string ConfigPath   = abc + "\\" + 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");
        }
Esempio n. 14
0
        public override bool Word2HTML(string sourcePath, string targetPath, string targetRelativeDirectory)
        {
            Microsoft.Office.Interop.Word.Application docApp = null;
            Microsoft.Office.Interop.Word.Document    doc    = null;
            object missing = Type.Missing;

            try
            {
                if (!sourcePath.EndsWith(".doc") && !sourcePath.EndsWith(".docx"))
                {
                    return(false);
                }


                // 缺省参数
                object Unknown     = Type.Missing;
                object object_True = true;

                //如果已存在,则重新生成
                if (File.Exists(targetPath))
                {
                    File.Delete(targetPath);
                }
                docApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                if (docApp == null)
                {
                    Log.DebugInfo("docApp空指针");
                }
                // 只读方式打开
                object readOnly = true;
                // 指定另存为格式(html)
                object formatHtml = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;

                object source = sourcePath;
                object target = targetPath;
                // 打开doc文件
                //Log.DebugInfo("打开doc文件..");
                Microsoft.Office.Interop.Word.Documents docs = docApp.Documents;
                if (docs == null)
                {
                    Log.DebugInfo("docs空指针");
                }
                if (Unknown == null)
                {
                    Log.DebugInfo("Unknown空指针");
                }
                //Log.DebugInfo("source object:"+ source);
                doc = docs.Open(ref source, ref Unknown, ref readOnly,
                                ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                                ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                if (doc == null)
                {
                    Log.DebugInfo("doc空指针");
                }
                //Log.DebugInfo("另存为HTML");
                //关闭拼写检查
                doc.SpellingChecked       = false;
                doc.ShowSpellingErrors    = false;
                doc.ShowGrammaticalErrors = false;


                Log.DebugInfo("word src Path : " + sourcePath);
                Log.DebugInfo("word target Path : " + targetPath);

                doc.SaveAs(ref target, ref formatHtml,
                           ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                           ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                //Log.DebugInfo("另存为html,成功");
            }
            catch (Exception e)
            {
                Log.LogInfo("word转换html:", e);
                return(false);
            }
            finally
            {
                //Log.DebugInfo("finally部分");
                if (doc != null)
                {
                    doc.Close(true, missing, missing);
                    doc = null;
                }
                if (docApp != null)
                {
                    docApp.Quit();
                    docApp = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                //Log.DebugInfo("word转化结束");
            }

            //Log.DebugInfo("修改html文件中的src元素的路径");
            changeSrc(targetPath, targetRelativeDirectory);
            return(true);


            //throw new NotImplementedException();
        }