如何:使用 .net 在 Word 上添加多个表格

发布于 2024-11-28 09:54:26 字数 964 浏览 0 评论 0原文

我尝试使用 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() ...等 如果在一张表上创建大量行,则仅添加到文档事件的一张表会失败 但不是多桌

I try to add multi tables within a word document using c#

// 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++;
            }
        }

this code add only one table , whenever in the second loop I should create another table and add the next item values on

I try to change the Range with set the myRange.start or myRange.setRange() ... etc
All fail only one table added to the document event if it create alot of rows on one table
but not multi table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じее 2024-12-05 09:54:26

该行在

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

msdn 上的文档声明:

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

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

    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() 也会在那里解决它。

The line

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

throws an exception the 2nd time it's executed with the message "The range cannot be deleted". This exception is swallowed by Word but does stop further execution. Addin a try/catch and setting a breakboint would have helped you.

I edited your code to the following to reproduce and find the exception that was raised:

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

The docs at msdn state:

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

What turns out to be needed is that you 'move' to the end of the range by collapsing it. If you do so you will run into another word issue that if you have 2 tables directly after eachother in a document word will automagically join them into 1 table. Your fixed code would end up adding more and more rows to 1 table and constantly overwrite the first few rows with the values. All of this leads to the following code that should fix your problem:

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

    }

One last warning is that the first line in this segment reads:

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

Adding a table to this range will work if the document is empty otherwise it will throw the same exception since we are not at the end in that case. A .Collapse() would resolve it there as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文