This article describes how to create a new Word document and insert a table using c#, it also shows how to format tables, and populate the tables with data.
follow these steps:
1. Start a new Windows application in C# language.
2. Add a reference to the Microsoft Word Object Library
3. Add a button to Form1 from Toolbox.
4. Create a Button1 click event.
5. Put this code on the click event of button1:
private void button1_Click(object sender, EventArgs e) { CreateTableInWordDocument(); }
private void CreateTableInWordDocument() { 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 objTable; Microsoft.Office.Interop.Word.Range wrdRng = objDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; objTable = objDoc.Tables.Add(wrdRng, 3, 4, ref oMissing, ref oMissing); objTable.Range.ParagraphFormat.SpaceAfter = 7; string strText; for (i = 1; i <= 3; i++) for (j = 1; j <= 4; j++) { strText = "Row" + i + " Coulmn" + j; objTable.Cell(i, j).Range.Text = strText; } objTable.Rows[1].Range.Font.Bold = 1; objTable.Rows[1].Range.Font.Italic = 1; objTable.Borders.Shadow = true; this.Close(); }
Thanks Hirendra, It was great help for me.
With the help of Third party component
Thanks for Sharing, Very informative, Here is another code samples that shows
how to build a new table with a table style applied
Document doc = new Document();
DocumentBuilder
builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
//We must insert at least one row first before setting any table formatting.
builder.InsertCell();
// Set the table style used based of the unique style identifier.
// Note that not all table styles are available when saving as .doc format.
table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1;
// Apply which features should be formatted by the style.
table.StyleOptions = TableStyleOptions.FirstColumn | TableStyleOptions.RowBands |
TableStyleOptions.FirstRow;
table.AutoFit(AutoFitBehavior.AutoFitToContents);
//Continue with building the table as normal.
builder.Writeln(“Item”);
builder.CellFormat.RightPadding = 40;
builder.InsertCell();
builder.Writeln(“Quantity (kg)”);
builder.EndRow();
builder.InsertCell();
builder.Writeln(“Apples”);
builder.InsertCell();
builder.Writeln(“20”);
builder.EndRow();
builder.InsertCell();
builder.Writeln(“Bananas”);
builder.InsertCell();
builder.Writeln(“40”);
builder.EndRow();
builder.InsertCell();
builder.Writeln(“Carrots”);
builder.InsertCell();
builder.Writeln(“50”);
builder.EndRow();
doc.Save(MyDir
+ “DocumentBuilder.SetTableStyle Out.docx”);
I am sure, you guys going to like it, Please download Aspose.Words for
.NET to use the above code : http://www.aspose.com/.net/word-component.aspx
Hi sir please my one resn I want to report ms-word in sqlserver data retrive in c# windows form sir any solution