using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.IO; using System.Data; using System.Reflection; using Word = Microsoft.Office.Interop.Word; using System.Runtime.InteropServices; using Microsoft.Office.Core; using System.Drawing; using System.Diagnostics; using System.Timers; using System.Windows.Forms; using Process = System.Diagnostics.Process; using System.Collections; namespace CreateTable2 { public partial class Form1 : Form { private void CreateTableInWordDocument() { int deleted = 0; object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc"; Microsoft.Office.Interop.Word._Application objWord; Microsoft.Office.Interop.Word._Document objDoc; objWord = new Microsoft.Office.Interop.Word.Application(); objWord.Visible = true; objDoc = objWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); int i = 0; int j = 0; Microsoft.Office.Interop.Word.Table objTable2; Microsoft.Office.Interop.Word.Range wrdRng2 = objDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; objTable2 = objDoc.Tables.Add(wrdRng2, 2, 4, ref oMissing, ref oMissing); objTable2.Range.ParagraphFormat.SpaceAfter = 0; // TH, 20Feb21, to set borders to the table objTable2.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; objTable2.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; string strText2; for (i = 1; i <= 2; i++) for (j = 1; j <= 4; j++) { strText2 = "Row_" + i + " Column_" + j; objTable2.Cell(i, j).Range.Text = strText2; } objTable2.Rows[1].Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorLightGreen; objTable2.Rows[1].HeadingFormat = -1; objTable2.ApplyStyleHeadingRows = true; //Insert a paragraph at the end of the document. Word.Paragraph objPara3; //define paragraph object object oRng2 = objDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; //go to end of the page objPara3 = objDoc.Content.Paragraphs.Add(ref oRng2); //add paragraph at end of document objPara3.Range.Text = " "; //add some text in paragraph objPara3.Range.Font.Size = 1f; objPara3.Format.SpaceAfter = 0; //defind some style Microsoft.Office.Interop.Word.Table objTable; Microsoft.Office.Interop.Word.Range wrdRng = objDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; objTable = objDoc.Tables.Add(wrdRng, 100, 4, ref oMissing, ref oMissing); objTable.Range.ParagraphFormat.SpaceAfter = 0; objTable.Range.Font.Size = 11f; // TH, 20Feb21, to set borders to the table objTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; objTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; string strText; for (i = 1; i <= 100; i++) for (j = 1; j <= 4; j++) { strText = "Row" + i + " Column" + j; objTable.Cell(i, j).Range.Text = strText; } objTable.Rows[1].Range.Font.Bold = 1; objTable.Rows[1].Range.Font.Italic = 1; // TH, 20Feb21, to set the background color of the Header of a table objTable.Rows[1].Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorLightBlue; objTable.Rows[1].HeadingFormat = -1; objTable.ApplyStyleHeadingRows = true; try { /* objPara3.Range.MoveStart(Word.WdUnits.wdCharacter, -2); deleted = objPara3.Range.Delete(); objPara3.Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd); int d = deleted; */ } catch(Exception e) { // MessageBox.Show("Exception: " + e.ToString()); } } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.CreateTableInWordDocument(); } private void Form1_Load(object sender, EventArgs e) { } } }