1

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

到现在为止一切正常。

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

更新


使用办公自动化

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

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

在此处输入图像描述

4

2 回答 2

8

使用选择来扫描文档在性能上应该是相当昂贵的。我建议使用以下代码:

        List<Word.Range> TablesRanges = new List<Word.Range>();

        wordApp = new Microsoft.Office.Interop.Word.Application();
        doc = wordApp.Documents.OpenNoRepairDialog(FileName: @"c:\AAAAA.docx", ConfirmConversions: false, ReadOnly: true, AddToRecentFiles: false, NoEncodingDialog: true);


        for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++)
        {
            Word.Range TRange = doc.Tables[iCounter].Range;
            TablesRanges.Add(TRange);
        }

        Boolean bInTable;
        for (int par = 1; par <= doc.Paragraphs.Count; par++)
        {
            bInTable = false;
            Word.Range r = doc.Paragraphs[par].Range;
            foreach (Word.Range range in TablesRanges)
            {
                if (r.Start >= range.Start && r.Start <= range.End)
                {
                    Console.WriteLine("In Table - Paragraph number " + par.ToString() + ":" + r.Text);
                    bInTable = true;
                    break;
                }

            }

            if (!bInTable)
                Console.WriteLine("!!!!!! Not In Table - Paragraph number " + par.ToString() + ":" + r.Text);
        }
于 2013-09-30T19:18:41.513 回答
2

我找到了相同的解决方法。方法如下。

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

//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
    }
  }
}
于 2012-08-16T05:36:48.390 回答