C#2010 Word-如何创建表之间没有空行 [英] C# 2010 Word - How to create tables without an empty line between

查看:127
本文介绍了C#2010 Word-如何创建表之间没有空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#2010,我需要打开word 2010模板,搜索书签并在其中插入表格.实际上,它应该是一个三部分表":一行包含两列,其后是包含五列的多行,最后是包含三列的单列.而且应该看起来像一张桌子,之间没有段落或空行.

Using C# 2010 I need to open a word 2010 template, search for a bookmark and insert a table there. Actually it should be a 'three-part-table': one row with two columns, after that multiple rows with five columns and finall three rows as single columns. And it should look as one table without paragraphs or empty lines between.

我在单词自动化方面的经验非常有限.我可以找到一些示例,该示例如何在书签中创建表-到目前为止没有问题-但是如何在之前的表之后立即添加新表...

My experience with word automation is quite limited. I can find examples how to create a table at a bookmark - no problem so far - but how can I add a new table immediately after the one before...

非常感谢您的帮助!

推荐答案

这应该可以解决问题.您不需要多个表即可拥有不同的列; Word让您有一个表,其中第一行有2列,接下来的3行有5列,最后3行只有1列. (您没有说五列需要多少行,所以我只用了3.)

This should do the trick. You don't need multiple tables in order to have different columns; Word lets you have a single table where the first row has 2 columns, the next 3 rows have 5 columns, and the last 3 rows only have 1 column. (You didn't say how many rows you needed with five columns, so I just went with 3.)

//Be sure to add this reference:
//Project>Add Reference>.NET tab>Microsoft.Office.Interop.Word

//open Word App
Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application();

//make it visible or it'll stay running in the background
msWord.Visible = true;

//open a new document based on the Word template.
//You shouldn't open the template directly using msWord.Documents.Open(path) unless you want to edit the template itself.
Microsoft.Office.Interop.Word.Document wordDoc = msWord.Documents.Add(@"c:\MyTemplate.dotx");

//find the bookmark
string bookmarkName = "BookmarkToFind";

if (wordDoc.Bookmarks.Exists(bookmarkName))
{
    Microsoft.Office.Interop.Word.Bookmark bk = wordDoc.Bookmarks[bookmarkName];

    //set the document's range to immediately after the bookmark.
    //If you want to add the table *into* the bookmark, it needs to be done differently.
    //This page has a good explanation of the differences between adding to the bookmark's range vs adding after the bookmark's range.
    //http://gregmaxey.mvps.org/word_tip_pages/insert_text_at_or_in_bookmark.html
    //It's a little more hassle because you have to re-add the bookmark after inserting into it,
    //so inserting after the bookmark is usually fine less you're going to be inserting text programmatically at the same bookmark a second time.
    Microsoft.Office.Interop.Word.Range rng = wordDoc.Range(bk.Range.End, bk.Range.End);

    //create a table with 8 rows and 5 columns into the range.
    Microsoft.Office.Interop.Word.Table tbl = wordDoc.Tables.Add(rng, 8, 5);

    //set the table's borders.
    tbl.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
    tbl.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

    //merge the cells in the first row down to 2 columns (Word's cells start at 1, not at 0).
    tbl.Cell(1, 1).Merge(tbl.Cell(1, 3));

    //distribute the columns evenly
    tbl.Rows[1].Select();
    msWord.Selection.Cells.DistributeWidth();

    //rows 2-5 already have 5 columns so don't touch them.

    //merge rows 6-8 into single-columns rows.
    for (int x = 6; x < 9; x++)
    {
        tbl.Cell(x,1).Merge(tbl.Cell(x,5));
    }

    //put the cursor in the table's first cell.
    rng=wordDoc.Range(tbl.Cell(1,1).Range.Start, tbl.Cell(1,1).Range.Start);
    rng.Select();

这篇关于C#2010 Word-如何创建表之间没有空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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