c# - 如何: Adding Multi tables on Word with .网

标签 c# .net 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++;
            }
        }

此代码仅添加一个表,每当在第二个循环中时,我应该创建另一个表并添加下一个项目值

我尝试通过设置 myRange.start 或 myRange.setRange() ...等来更改范围 如果在一张表上创建大量行,则仅添加到文档事件的一张表会失败 但不是多表

最佳答案

线路

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

第二次执行时抛出异常,并显示消息“无法删除范围”。此异常被 Word 吞噬,但会停止进一步执行。添加 try/catch 并设置断点会对您有所帮助。

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

    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);
        }
 }

docs at msdn状态:

Required Range object. The range where you want the table to appear. The table replaces the range, if the range isn't collapsed.

事实证明,您需要通过折叠范围来“移动”到范围的末尾。如果这样做,您将遇到另一个单词问题,即如果文档中有两个紧邻的表格,单词会自动将它们连接到一个表格中。您的固定代码最终会向一个表添加越来越多的行,并不断用值覆盖前几行。所有这些都会导致以下代码应该可以解决您的问题:

    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);
        }

    }

最后一个警告是该段中的第一行内容为:

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

如果文档为空,则向此范围添加表格将起作用,否则它将引发相同的异常,因为在这种情况下我们还没有到达末尾。 .Collapse() 也会在那里解决它。

关于c# - 如何: Adding Multi tables on Word with .网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6996242/

相关文章:

c# - 如何在按钮单击时调用异步?

.net - .Net 框架的早期历史是什么?

.net - 在 Entity Framework 5 中将代码生成从无更改为默认值后出现错误

c# - 如何逐行阅读 MS Word 段落和表格内容

c# - 调用私有(private)方法保留调用栈

c# - C# 如何*确切地*将 double 转换为十进制?

java - Exchange Web 服务通过 "Message-ID" header 查找 EmailMessage

c# - ADFS - Windows 集成 OR Forms 身份验证

excel - VBA - 通过优化 Word Table 每一页上第一行的定位索引。 Excel

java - 在firefox中的java小程序中查看word office文档