JG Vimalan's Blog

It is all about coding!

Add rows to table in MS Word document using C#

I got a task where I need to dynamically add rows to a table in MS Word document using C#. The row count may differ based on user. In order to achieve this, I have added reference to Microsoft.Office.Interop.Word in my ASP.Net web application.

I have added the table head part in the MS Word document to keep the things simple. The MS Word document looks like,

 

using System;
using Microsoft.Office.Interop.Word;

namespace demo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
object missing = System.Reflection.Missing.Value;
object setFalse = false;
object savechanges = true;

var wordApp = new ApplicationClass();

object fileName = @”C:\sample.doc”;

Document adoc = wordApp.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);

adoc.Activate();

Tables tables = adoc.Tables;
if (tables.Count > 0)
{
//Get the first table in the document
Table table = tables[1];

int rowsCount = table.Rows.Count;
int coulmnsCount = table.Columns.Count;

for (int i = 0; i < 25; i++)
{
Row row = table.Rows.Add(ref missing);

for (int j = 1; j<=coulmnsCount; j++)
{
row.Cells[j].Range.Text = string.Format(“{0}{1}”, i, j);
row.Cells[j].WordWrap = true;
row.Cells[j].Range.Underline = WdUnderline.wdUnderlineNone;
row.Cells[j].Range.Bold = 0;
}
}
}

object sFile = @”C:\result.doc”; //save the updated document
adoc.SaveAs(ref sFile, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing);

adoc.Close(ref savechanges, ref missing, ref missing);
wordApp.Quit(ref savechanges, ref missing, ref missing);
}
}
}

Once the code is executed, the new rows have been added to the document. The result table is as follows,

 

Advertisements

February 8, 2011 - Posted by | ASP.NET, C#.NET

7 Comments »

  1. I was looking for this… perfect!

    Comment by Muthu | February 25, 2011 | Reply

  2. Thank for your sharing.

    Comment by Neil | April 23, 2011 | Reply

  3. thank you

    Comment by ashwini | December 17, 2012 | Reply

  4. I want to insert a row the above code will jst add a row at end only

    Comment by krishnakishore | June 5, 2013 | Reply

  5. Thank you it helped me a lot.

    Comment by Deepak | June 21, 2013 | Reply

  6. @krishnakishore :
    Use below line so that you can add rows at your desired point in table.

    object beforeRow = tables[i].Rows[3];
    Row row = table.Rows.Add(ref beforeRow);

    Comment by Deepak | June 21, 2013 | Reply

  7. This is exactly what I needed.
    Thanks!

    Comment by Cody O. | November 7, 2014 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: