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

How : Adding Multi tables on Word with .net(如何:使用 .net 在 Word 上添加多表)

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

问题描述

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

解决方案

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.

这篇关于如何:使用 .net 在 Word 上添加多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何:使用 .net 在 Word 上添加多表

基础教程推荐