C# 如何逐行阅读MS Word段落和表格内容
我正在使用Microsoft.Office.Interop.word阅读C#(3.5)中的word文档。逐行读取,将行拆分为数组[],处理行中的每个字,并根据某些业务逻辑替换某些字,替换字后,用转换行替换整行 到目前为止,一切都很顺利 现在我有一些word文档,它们有段落和表格。我希望逐个读取表中的每一列,并替换特定列中该列的内容 更新C# 如何逐行阅读MS Word段落和表格内容,c#,.net,ms-word,office-interop,C#,.net,Ms Word,Office Interop,我正在使用Microsoft.Office.Interop.word阅读C#(3.5)中的word文档。逐行读取,将行拆分为数组[],处理行中的每个字,并根据某些业务逻辑替换某些字,替换字后,用转换行替换整行 到目前为止,一切都很顺利 现在我有一些word文档,它们有段落和表格。我希望逐个读取表中的每一列,并替换特定列中该列的内容 更新 使用办公自动化 1. Opening word file. 2. Moving cursor to top of the document 3. Select
使用办公自动化
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
我想处理所有列的数据。
有没有办法确定内容是段落还是表格
我找到了同样的解决方法。 方法如下所示。
1.使用
WordApp.Documents.Open()打开Word文档
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
}
}
}
//处理文档中的所有段落
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);
//跳过表内容
if(doc.ActiveWindow.Selection.get_信息(WdInformation.wdendorfrangecolumnnumber.ToString()!=“-1”)
{
while(doc.ActiveWindow.Selection.get_信息(WdInformation.wdendorfrangecolumnnumber.ToString()!=“-1”)
{
如果(doc.ActiveWindow.Selection.Bookmarks.Exists)(@“\EndOfDoc”))
打破
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;
}
//处理文档中的所有表
对于(inticounter=1;iCounter,我找到了一个解决方法。
方法如下所示。
1.使用WordApp.Documents.Open()打开Word文档
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
}
}
}
//处理文档中的所有段落
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);
//跳过表内容
if(doc.ActiveWindow.Selection.get_信息(WdInformation.wdendorfrangecolumnnumber.ToString()!=“-1”)
{
while(doc.ActiveWindow.Selection.get_信息(WdInformation.wdendorfrangecolumnnumber.ToString()!=“-1”)
{
如果(doc.ActiveWindow.Selection.Bookmarks.Exists)(@“\EndOfDoc”))
打破
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;
}
//处理文档中的所有表
对于(int-iCounter=1;iCounter,使用选择扫描文档在性能上应该非常昂贵。
我建议使用以下代码:
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);
}
List TablesRanges=new List();
wordApp=新的Microsoft.Office.Interop.Word.Application();
doc=wordApp.Documents.OpenNoRepairDialog(文件名:@“c:\AAAAA.docx”,ConfirmConversions:false,ReadOnly:true,AddToRecentFiles:false,NoEncodingDialog:true);
对于(int-iCounter=1;iCounter,使用选择扫描文档在性能上应该非常昂贵。
我建议使用以下代码:
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);
}
List TablesRanges=new List();
wordApp=新的Microsoft.Office.Interop.Word.Application();
doc=wordApp.Documents.OpenNoRepairDialog(文件名:@“c:\AAAAA.docx”,ConfirmConversions:false,ReadOnly:true,AddToRecentFiles:false,NoEncodingDialog:true);
对于(int iCounter=1;iCounter。有关从表中检索值的信息,请参见此。除了由@gout链接的答案外,Word interop的表接口还包含一个Columns属性,该属性与该链接中使用的Rows属性非常相似。有关从表中检索值的信息,请参见此信息。除了由@gout链接的答案,表interfaWord interop的ce包含一个Columns属性,该属性与该链接中使用的Rows属性非常相似。请使用完整代码更新答案。请使用完整代码更新答案。