我正在使用Microsoft.Office.Interop.Word
在C#(3.5)中阅读word文档。逐行读取,将行拆分为数组[]并处理行的每个单词,并根据一些业务逻辑替换一些单词并在替换单词后,用转换后的行替换整行。
直到现在每件事都运转良好。
现在我有一些word文档,这些文档有段落和表格。我想逐一阅读表中的每一列,并替换特定列中列的内容。
更新
使用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
我想处理所有列的数据。 有没有办法确定内容是段落还是表格?
答案 0 :(得分:4)
使用选择扫描文档的性能应该非常昂贵。 我建议使用以下代码:
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);
}
答案 1 :(得分:2)
我找到了同样的解决方法。
方法如下:
1.使用WordApp.Documents.Open()
打开Word文档
2.使用Selection.MoveDown
逐行遍历Document
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
}
}
}