Word 2010表

时间:2012-10-31 17:07:24

标签: c#

我有一个充满书签的单词模板,但是当我试图插入一个表时我被卡住了...首先我发出了一个COMException,说集合中请求的成员不存在... ive pressume这意味着设置的书签的名称与我现在调用的表格不同。无论如何,表格根本不显示..我已经在数据输入表格之前设置了格式...如下所示:

   // Insert Table


            Word.Table tbl1 = this.Tables[1];
            Tables.Add(Range: tbl1.Range, NumColumns: 2, NumRows: 2);
            tbl1.Range.Font.Size = 10;
            tbl1.Range.Font.Name = "Georgia";
            tbl1.Range.Font.Bold.Equals(true);
            tbl1.Range.Font.ColorIndex = Word.WdColorIndex.wdBlue;
            tbl1.Range.Cells.Shading.Texture = Word.WdTextureIndex.wdTexture10Percent;
            tbl1.Range.Cells.Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdBlue;
            tbl1.Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
            tbl1.Rows.SetHeight(RowHeight: 24, HeightRule: Word.WdRowHeightRule.wdRowHeightAtLeast);
            tbl1.Columns[1].SetWidth(ColumnWidth: 170, RulerStyle: Word.WdRulerStyle.wdAdjustNone);
            tbl1.Columns[2].SetWidth(ColumnWidth: 310, RulerStyle: Word.WdRulerStyle.wdAdjustNone);
            tbl1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
            tbl1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleNone;

            // end of table insert

然后填充表格的代码是......

 if (multipleLimits.Equals(false))
            {
                tbl1.Cell(1, 1).Range.Text = "Indemnity Limit:";
            }
            else
            {
                tbl1.Cell(1, 1).Range.Text = IndemlimitsText(iIndemnLimit).ToString();
            }


            switch (typeOfInsID)
            {
                case "4":
                    tbl1.Cell(1, 1).Range.Text = "Public/Products Liability:";
                    break;
            }

            tbl1.Cell(2, 1).Range.Text = "Excess:";

            if (multipleLimits.Equals(false))
            {
                tbl1.Cell(1, 2).Range.Text = sCurType + iIndemnLimit;
            }
            else
            {
                tbl1.Cell(1, 2).Range.Text = stripIndemLimitCode(iIndemnLimit).ToString();
            }
等等...... 我的问题是,如果表格显示即使没有数据要解析...说我是否想逐行测试解析数据?或填充表的代码是否需要存在并且正确的表格才能显示???
我希望我能够过多地絮絮叨叨地混淆我的问题。
谢谢你们!

1 个答案:

答案 0 :(得分:0)

答案是肯定的,表格应该显示,即使所有单元格都是空的。

我认为你的范围是问题所在
object endOfDoc = "\\endofdoc";
object missing = System.Reflection.Missing.Value;
string totRows = 2;
string totColumns = 2;

word.Range wrdRng = doc.Bookmarks.get_Item(ref endOfDoc).Range;
oTable = doc.Tables.Add(wrdRng, totRows, totColumns, ref missing, ref missing);

会将包含2列和2行的表添加到文档的末尾。您尝试添加到表的范围(tbl1.Range),但这可以是表本身内部的内部范围(包含您的单元格)。您想要添加到表格之外的段落或其他内容。 endOfDoc是一个书签,可用于将内容添加到文档的末尾。

因为我不知道您使用的是哪个模板,所以您可以先尝试使用,然后找出所需的正确书签名称。这可以用与上面代码中的endOfDoc相同的方式定义,