方法:使用.net在Word上添加多表

我尝试使用c#在word文档中添加多个表

// Tables is a list of items which I want to present each in a table foreach (List ClassTable in Tables) { // tbl is a "Microsoft.Office.Interop.Word.Table" // myRange is like MyDoc.Range(ref missing, ref missing) tbl = MyDoc.Tables.Add(myRange, ClassTable.Count(), 3, missing, missing); tbl.Borders.Enable = 1; RowCounter = 1; foreach (string[] item in TableContent) { ColumnCounter = 1; foreach (string str in item) { tbl.Cell(RowCounter, ColumnCounter).Range.Text = str; ColumnCounter++; } RowCounter++; } } 

这段代码只添加一个表,每当在第二个循环中我应该创建另一个表并添加下一个项值

我尝试通过设置myRange.start或myRange.setRange()…等来更改范围如果它在一个表上创建了很多行而不是多个表,那么只会将一个表添加到文档事件中

这条线

 tbl = MyDoc.Tables.Add(myRange, ClassTable.Count(), 3, missing, missing); 

在第二次执行时抛出exception,并显示消息“无法删除范围”。 Word会吞下此exception但会停止进一步执行。 添加一个try / catch并设置一个breakboint会帮助你。

我将您的代码编辑为以下内容以重现并查找引发的exception:

  var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range(); foreach (List> ClassTable in new List>> { new List> { new List { "A" }, new List { "B" } }, new List> { new List { "C" }, new List { "D" } } }) { // tbl is a "Microsoft.Office.Interop.Word.Table" // myRange is like MyDoc.Range(ref missing, ref missing) Microsoft.Office.Interop.Word.Table tbl = null; try { tbl = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(myRange, ClassTable.Count(), 3); tbl.Borders.Enable = 1; int RowCounter = 1; foreach (var item in ClassTable) { int ColumnCounter = 1; foreach (string str in item) { tbl.Cell(RowCounter, ColumnCounter).Range.Text = str; ColumnCounter++; } RowCounter++; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } 

msdn状态的文档 :

必需的Range对象。 您希望表格显示的范围。 如果范围未折叠,则表格将替换范围。

结果需要的是你通过折叠它“移动”到范围的末尾。 如果你这样做,你将遇到另一个单词问题,如果在文档单词中彼此之后直接有2个表,它们会自动将它们连接到1个表中。 您的固定代码最终会向1个表添加越来越多的行,并不断用值覆盖前几行。 所有这些导致以下代码可以解决您的问题:

  var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range(); foreach (List> ClassTable in new List>> { new List> { new List { "A" }, new List { "B" } }, new List> { new List { "C" }, new List { "D" } } }) { // tbl is a "Microsoft.Office.Interop.Word.Table" // myRange is like MyDoc.Range(ref missing, ref missing) Microsoft.Office.Interop.Word.Table tbl = null; try { tbl = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(myRange, ClassTable.Count(), 3); tbl.Borders.Enable = 1; int RowCounter = 1; foreach (var item in ClassTable) { int ColumnCounter = 1; foreach (string str in item) { tbl.Cell(RowCounter, ColumnCounter).Range.Text = str; ColumnCounter++; } RowCounter++; } // Move to the end myRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd); // Now add something behind the table to prevent word from joining tables into one myRange.InsertParagraphAfter(); // gosh need to move to the end again myRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } 

最后一个警告是该段的第一行是:

 var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range(); 

如果文档为空,则将表添加到此范围将起作用,否则将抛出相同的exception,因为在这种情况下我们不在最后。 A.Collapse()也会解决它。