如何逐行阅读MS Word段落和表格内容 [英] How to read MS Word paragraph and table content line by line

查看:224
本文介绍了如何逐行阅读MS Word段落和表格内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft.Office.Interop.Word读取C#(3.5)中的Word文档.逐行读取,将行拆分为array []并处理行中的每个单词,并基于某些业务逻辑替换了某些单词,并在替换了单词之后,将整个行替换为转换后的行.

I am reading word document in C# (3.5) using Microsoft.Office.Interop.Word. Reading line by line, splitting line into array[] and processing every word of line and based on some business logic replacing some words and after the replacement of words, replacing the complete line with converted line.

到现在为止,一切正常.

Till now Every thing is working fine.

现在我有一些word文档,这些文档具有段落和表格.我想一一阅读表的每一列,并替换特定列中的列内容.

Now i have some word documents, those are having paragraph and tables. I want to read every Column of the table one by one and replace the content of the column in particular column.

更新

使用Office自动化

Using Office Automation

1. Opening word file.
2. Moving cursor to top of the document
3. Selecting first line using (`wordApp.Selection.endKey`) and processing all words
4. After processing the words replacing the selected line with the processed line.
5. Using wordApp.Selection.MoveDown(ref lineCount, ref countPage, ref MISSING);    
   moving next line processed further.

问题: 1.读取表时,使用wordApp.Selection.endKey

Problem: 1. When reading table it reads only first column when using wordApp.Selection.endKey

我要处理所有列的数据. 有什么方法可以识别内容是段落还是表格?

I want to process all column's data. Is there any way to identify whether content is paragraph or table?

推荐答案

我找到了解决该问题的方法. 方法在下面列出.

1.使用WordApp.Documents.Open()
打开Word文档 2.使用Selection.MoveDown逐行遍历文档
3.跳过表格单元格的内容
4.最后仅处理文档表

I found a workaround for the same. Approach is listed below.

1. Open the Word Document using WordApp.Documents.Open()
2. Using Selection.MoveDown to traverse the Document line by line
3. Skipping the content of the Table's Cells
4. At last processing only Tables of the document

//Process all Paragraphs in the documents
while (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc") == false)
{
  doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove);
  doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);

  //Skiping table content
  if (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1")
  {
    while (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1")
    {
      if (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc"))
        break;

      doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove);
      doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);
    }
    doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);
  }

  doc.ActiveWindow.Selection.EndKey(ref wdLine, ref wdExtend);
  currLine = doc.ActiveWindow.Selection.Text;
}

//Processing all tables in the documents
for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++)
{
  foreach (Row aRow in doc.Tables[iCounter].Rows)
  {
    foreach (Cell aCell in aRow.Cells)
    {
      currLine = aCell.Range.Text;
      //Process Line
    }
  }
}

这篇关于如何逐行阅读MS Word段落和表格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆