当前位置: 首页>>代码示例>>C#>>正文


C# Microsoft.Office.Interop.Word.Application.GetType方法代码示例

本文整理汇总了C#中Microsoft.Office.Interop.Word.Application.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Microsoft.Office.Interop.Word.Application.GetType方法的具体用法?C# Microsoft.Office.Interop.Word.Application.GetType怎么用?C# Microsoft.Office.Interop.Word.Application.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Office.Interop.Word.Application的用法示例。


在下文中一共展示了Microsoft.Office.Interop.Word.Application.GetType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Convertion2Html

        /// <summary>
        /// 将doc文档转换成html
        /// </summary>
        /// <param name="FilePath">虚拟路径文件名</param>
        /// <param name="NewHtmlName">新的html文件名</param>
        /// <param name="StrHtml">获取的Html</param>
        /// <param name="IsDelDocFile">是否删除doc文件</param>
        /// <param name="IsDelHtmlFile">是否删除html文件</param>
        /// <returns>如果返回false,则NewHtmlName和StrHtml均为空</returns>
        public bool Convertion2Html(string FilePath, out string NewHtmlName, out string StrHtml, bool IsDelDocFile, bool IsDelHtmlFile)
        {
            NewHtmlName = "";
            StrHtml = "";
            if (FilePath.Trim() == "")
            {
                return false;
            }

            //必须先关闭WINWORD
            try
            {
                System.Diagnostics.Process[] CloseID = System.Diagnostics.Process.GetProcessesByName("WINWORD");

                for (int i = 0; i < CloseID.Length; i++)
                {
                    if (!CloseID[i].CloseMainWindow())
                    {
                        CloseID[i].Kill();
                    }
                }
            }
            catch { }
            //绝对路径,同时将后缀名定位小写doc,防止大写
            string RealFileNamePath = HttpContext.Current.Server.MapPath(FilePath);
            RealFileNamePath = RealFileNamePath.Substring(0, RealFileNamePath.Length - 3) + "doc";
            RealFileNamePath = "d:\\2.doc";
            //调用word类库
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            Type wordType = word.GetType();
            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();
            //读取doc文档
            Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
            try
            {
               doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
               System.Reflection.BindingFlags.InvokeMethod,null, docs, new Object[] { RealFileNamePath, true, true });
            }
            catch
            {
                try
                {
                    wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
                        null, word, null);
                }
                catch { }
                return false;
            }

            //   转换格式,另存为
            Type docType = doc.GetType();
            object saveFileName = RealFileNamePath.Replace(".doc", ".htm");

            try
            {
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
                ///其它格式:
                ///wdFormatHTML
                ///wdFormatDocument
                ///wdFormatDOSText
                ///wdFormatDOSTextLineBreaks
                ///wdFormatEncodedText
                ///wdFormatRTF
                ///wdFormatTemplate
                ///wdFormatText
                ///wdFormatTextLineBreaks
                ///wdFormatUnicodeText
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch
            {
                //   退出 Word
                try
                {
                    wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
                        null, word, null);
                }
                catch { }
                return false;
            }
            //返回的数据
            NewHtmlName = FilePath.Replace(".doc", ".htm");
            //一定要等待几秒再操作,否则由于缓存的文件没有写入,会导致异常
            System.Threading.Thread.Sleep(3000);
            StrHtml = this.GetFileContent(NewHtmlName);
            //根据需要删除文件
            if (IsDelDocFile)
            {
                System.IO.File.Delete(RealFileNamePath);
//.........这里部分代码省略.........
开发者ID:inspire88,项目名称:TcportGroupOA,代码行数:101,代码来源:DocOpt.cs


注:本文中的Microsoft.Office.Interop.Word.Application.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。