如何:在Word中添加多桌与.NET [英] How : Adding Multi tables on Word with .net

查看:123
本文介绍了如何:在Word中添加多桌与.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Word文档中添加多表C#

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

我试图改变与集myRange.start或myRange.setRange()...等的范围
全部失败只有一个表添加到文档事件,如果它在一个表上
创建行的很多,但不支持多表

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

推荐答案

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



抛出一个异常,它的消息的范围不能被删除执行的第二次。此异常由字吞下但并停止进一步执行。插件一个try / catch和设置breakboint会帮助你的。

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



在MSDN 状态文档:

所需的范围对象。要将表出现的范围。
中的表替换的范围内,如果该区域未折叠。

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

什么原来有需要的是你被倒塌的是移动的范围的结束。如果你这样做你会遇到另一个词的问题,如果你有2个表后直接海誓山盟在文档字会被自动它们连接成1台。您的固定码最终会增加越来越多的行1表,并不断改写的价值观排第几。所有这一切都导致了下面的代码应该解决您的问题:

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



添加一个表来此范围内,如果该文件是空的,否则它会抛出同样的异常,因为将工作我们不是在这种情况下,结束。一个.Collapse()会解决这个问题有作为。

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.

这篇关于如何:在Word中添加多桌与.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆