C # программа, которая использует взаимодействие Microsoft Word - PullRequest
0 голосов
/ 03 декабря 2018

Документ Word: word.doc

One

Two

three

Программа C #, использующая взаимодействие с Microsoft Word

 using System;
 using Microsoft.Office.Interop.Word;

 class Program
 {
   static void Main()
 {
    // Open a doc file.
    Application application = new Application();
    Document document = application.Documents.Open("C:\\word.doc");

    // Loop through all words in the document.
    int count = document.Words.Count;
    for (int i = 1; i <= count; i++)
    {
        // Write the word.
        string text = document.Words[i].Text;
        Console.WriteLine("Word {0} = {1}", i, text);
    }
    // Close word.
    application.Quit();
  }
  }

Вывод:

Word 1 = One
Word 2 =
Word 3 = Two
Word 4 =
Word 5 = three
Word 6 =

1 Ответ

0 голосов
/ 03 декабря 2018

попробуйте это - добавлен еще один счетчик для real слов

    internal class Program
    {
        private static void Main(string[] args)
        {
            // Open a doc file.
            Application application = new Application();
            Document document = application.Documents.Open("C:\\temp\\word.doc");

            // Loop through all words in the document.

            int k = 1;

            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                // Write the word.
                string text = document.Words[i].Text.Trim();

                if (!string.IsNullOrEmpty(text))
                {
                    Console.WriteLine("Word {0} = {1}", k, text);
                    k++;
                }
            }

            Console.ReadLine();
            // Close word.
            application.Quit();
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...