C#读取word文档文本

读取word,首先得添加引用,不同的word版本对应着不同的引用

部分版本对应引用如下:

Microsoft Word 11.0 object library对应Office2003
Microsoft Word 12.0 object library对应Office2007
Microsoft Word 14.0 object library对应Office2010
Microsoft Word 15.0 object library对应Office2013

 

由于我电脑的版本是word 2007,故添加Microsoft Word 12.0 Object Library,添加方法,右击项目解决方案,选择 Add Reference,弹出对话框如下图:

 C#读取word文档文本_第1张图片

 

再使用下面两个命名空间,如图:

 

 

完整代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Office;

using Word;

namespace ReadWordText

{

    class Program

    {

        static void Main(string[] args)

        {

 

            Application app = new Application();

            Document doc = null;

            object unknow = Type.Missing;

            object ReadOnly = false;//是否只能读

            object encoding = Encoding.UTF8;//UTF8编码

            app.Visible = false;

            string str = @"C:\Users\zxy\Desktop\读取word文档.doc";//文档的路径

            object file = str;

            try

            {

                doc = app.Documents.Open(ref file,

               ref unknow, ref ReadOnly, ref unknow, ref unknow,

               ref unknow, ref unknow, ref unknow, ref unknow,

               ref unknow, ref encoding, ref unknow, ref unknow,

               ref unknow, ref unknow, ref unknow);

 

                //读取第几段内容(空白行、各级标题等均作为一段来算)  

                //string strParaghaph = doc.Paragraphs[4].Range.Text.Trim();

 

                //读取第几句内容(空白行、各级标题等都作为一句来算)

                // string strSentence = doc.Sentences[5].Text;

 

 

                //读取整篇内容

                int sentencesLength = doc.Paragraphs.Count;//文档的总段数

                for (int sen = 1; sen <= sentencesLength; sen++)

                {

                    string strSence = doc.Paragraphs[sen].Range.Text;//获取每段内容

                    Console.WriteLine(strSence);

                }

            

 

 

            }

            catch (Exception)

            {

 

                Console.WriteLine("无法读取到文本");

            }

           

          

            Console.ReadKey();

 

 

        }

    }

}

 

你可能感兴趣的:(C#编程,C#读取word文本,C#,读取word,word)