rt

解决方案 »

  1.   

    给你一个类using Microsoft.Office.Interop.Word;
    class opertateWord
        {
            public string contentText = "";
            public void WordToPdf(string sourcePath)
            {
                object paramMissing = Type.Missing;
                Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word._Document wordDocument = null;
                try
                {
                    object paramSourceDocPath = sourcePath;
                    wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing,
                        ref paramMissing);
                    string text;
                    if (wordDocument != null)
                    {
                        text = wordDocument.Content.Text;//这里只取word文本,要取其它内容也可以在这里选择。
                        contentText = text;
                    }
                }            catch (Exception e)
                {
                    Console.WriteLine("错误提示:" + e.Message);
                }            finally
                {
                    if (wordDocument != null)
                    {
                        wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                        wordDocument = null;
                    }
                    if (wordApplication != null)
                    {
                        wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                        wordApplication = null;
                    }
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
        }
      

  2.   

    顶,不过一楼不够详细,
     #region 读取        /// <summary>
            /// 读取一段
            /// </summary>
            /// <param name="ParaCount">/第几段</param>
            /// <param name="ReadOnly">/是否只读</param>
            /// <returns></returns>
            public string Read(int ParaCount, bool ReadOnly)
            {            string txt = Convert.ToString(MyDoc.Paragraphs[ParaCount].Range.Text);
                return txt;        }
            /// <summary>
            /// 读取指定长度
            /// </summary>
            /// <param name="Start">/开始位置</param>
            /// <param name="End">/结束位置</param>
            /// <returns></returns>
            public string Read(int Start, int End)
            {            object start = Start;
                object end = End;
                string txt = "";
                txt = Convert.ToString(MyDoc.Range(ref start, ref end).Text);
                return txt;
            }
            #endregion
            #region 表相关操作
            /// <summary>
            /// 读取表的位置
            /// </summary>
            /// <param name="TableIndex">/哪个表</param>
            public Word.Range TableLocation(int TableIndex)
            {            MyRange = MyDoc.Tables[TableIndex].Range;
                return MyRange;
            }
            /// <summary>
            /// 当前文档内有几个表
            /// </summary>
            /// <returns></returns>
            public int TableCount()
            {
                return MyDoc.Tables.Count;
            }
            /// <summary>
            /// 返回一个表的内容
            /// </summary>
            /// <param name="TableIndex">/哪个表</param>
            /// <returns></returns>
            public Word.Table Read(int TableIndex)
            {
                return MyDoc.Tables[TableIndex];
            }
            #endregion