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

Ash*_*OoO 2 .net c# ms-word range office-interop

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

// Tables is a list of items which I want to present each in a table
foreach (List<string[]> 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++;
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

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

Edd*_*ddy 5

这条线

tbl = MyDoc.Tables.Add(myRange, ClassTable.Count(), 3, missing, missing); 
Run Code Online (Sandbox Code Playgroud)

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

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

    var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
    foreach (List<List<string>> ClassTable in new List<List<List<string>>> { new List<List<string>> { new List<string> { "A" }, new List<string> { "B" } }, new List<List<string>> { new List<string> { "C" }, new List<string> { "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);
        }
 }
Run Code Online (Sandbox Code Playgroud)

msdn状态的文档:

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

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

    var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
    foreach (List<List<string>> ClassTable in new List<List<List<string>>> { new List<List<string>> { new List<string> { "A" }, new List<string> { "B" } }, new List<List<string>> { new List<string> { "C" }, new List<string> { "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);
        }

    }
Run Code Online (Sandbox Code Playgroud)

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

var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
Run Code Online (Sandbox Code Playgroud)

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