程序中需要用代码新建一个word的文档,并要在文档的合适位置插入一个5列3行的表格,目前可以实现创建word的功能,但不知道如何插入表格,希望各位大哥帮帮忙,指点一下.

解决方案 »

  1.   

    string strFileName = "d:\\test\\test.doc";
    Object str = strFileName; Word.ApplicationClass WordApp = new Word.ApplicationClass();
    WordApp.Visible = false;
    Word.Document WordDoc = null;
    try
    {
    Object o = Type.Missing;
    WordDoc = WordApp.Documents.Open(ref str,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
    //WordDoc.Content.Delete(ref o,ref o);
    WordDoc.Paragraphs.Last.Range.Text = "leiting";
    Word.Range range = WordDoc.Range(ref o,ref o);
    WordDoc.Tables .Add(range,5,3,ref o,ref o);
    Word.Table WordTab = WordDoc.Tables.Item(1);
    WordTab.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
    WordTab.Range.Rows .Alignment = Word.WdRowAlignment.wdAlignRowCenter;
    //range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    WordTab.Range.Cells.Height = 25;
    //WordTab.Range.Cells.Width = 100;
    WordTab.Range.Font.Size = 12;
    WordTab.Range.Font.Name = "宋体";
    WordTab.Range.Font.Bold = 3;
    //string i = WordTab.Range.Font.Bold.ToString();
                                  }