c#调用Wrod进行简繁转换相关代码

1.在Com组件处引用 Com Microsoft Word 11.0 Object Library(安装Office时必须选择)
2.添加2个using
  using Microsoft.Office.Interop.Word;
  using System.Reflection;

3.插入以下代码,用静态方法调用即可

#region 简体转繁体代码
        /// <summary>
        /// 简体转换为繁体
        /// </summary>
        /// <param name="src">输入简体文字</param>
        /// <returns>返回繁体文字</returns>
        static string CHS2CHT(string src)
        {
            string des = "";
            _Application appWord = new Microsoft.Office.Interop.Word.Application();
            object template = Missing.Value;
            object newTemplate = Missing.Value;
            object docType = Missing.Value;
            object visible = true;
            Document doc = appWord.Documents.Add(ref template, ref newTemplate, ref docType, ref visible);
            appWord.Selection.TypeText(src);
            appWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
            appWord.ActiveDocument.Select();
            des = appWord.Selection.Text;
            object saveChange = 0;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;
            appWord.Quit(ref saveChange, ref originalFormat, ref routeDocument);
            doc = null;
            appWord = null;
            GC.Collect();//进程资源释放

            return des;
        } 
        #endregion

        #region 繁体转简体代码
        /// <summary>
        /// 繁体转换为简体
        /// </summary>
        /// <param name="src">输入繁体文字</param>
        /// <returns>返回简体文字</returns>
        static string CHT2CHS(string src)
        {
            string des = "";
            _Application appWord = new Microsoft.Office.Interop.Word.Application();
            object template = Missing.Value;
            object newTemplate = Missing.Value;
            object docType = Missing.Value;
            object visible = true;
            Document doc = appWord.Documents.Add(ref template, ref newTemplate, ref docType, ref visible);
            appWord.Selection.TypeText(src);
            appWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, true, true);
            appWord.ActiveDocument.Select();
            des = appWord.Selection.Text;
            object saveChange = 0;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;
            appWord.Quit(ref saveChange, ref originalFormat, ref routeDocument);
            doc = null;
            appWord = null;
            GC.Collect();//进程资源释放

            return des;
        } 
        #endregion
View Code

 

 

posted @ 2013-09-15 23:13  wbkboy  阅读(237)  评论(0)    收藏  举报