This sample code shows how to fill values into Tables of MSWord File.
Eg If you have two table exist in word .doc file then you can fill this value
1)Firstly add the Namespace for Word Document in your file.....
2) Use the code as:
rivate void CreateWordTable() { // Clear the current contents. Object start = Type.Missing; Object end = Type.Missing; Object unit = Type.Missing; Object count = Type.Missing; ThisDocument.Range(ref start, ref end). Delete(ref unit, ref count);
// Move to start of document. start = 0; end = 0; Word.Range rng = ThisDocument.Range(ref start, ref end);
// Insert Title and paragraph marks. rng.InsertBefore("Top Ten Products"); rng.Font.Name = "Verdana"; rng.Font.Size = 16; rng.InsertParagraphAfter(); rng.InsertParagraphAfter(); rng.SetRange(rng.End, rng.End);
// Add the table. Object defaultTableBehavior = Type.Missing; Object autoFitBehavior = Type.Missing; rng.Tables.Add(ThisDocument.Paragraphs[2].Range, 1, 2, ref defaultTableBehavior, ref autoFitBehavior);
// Set variable to point to new table. Word.Table tbl = ThisDocument.Tables[1];
// Format the table. tbl.Range.Font.Size = 12; tbl.Range.Font.Name = "Verdana"; tbl.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; tbl.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDouble;
// Set the column widths. tbl.Columns[1].SetWidth( ThisApplication.InchesToPoints((float)2.5), Word.WdRulerStyle.wdAdjustNone); tbl.Columns[2].SetWidth( ThisApplication.InchesToPoints((float)1.5), Word.WdRulerStyle.wdAdjustNone); tbl.Cell(1, 1).Range.Text = "Product Name";
// Right-align second column. Word.Range rngCell = tbl.Cell(1, 2).Range; rngCell.Text = "Unit Price"; rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; }
// C# string strMDB = "C:\\program files\\microsoft office\\" + "Office11\\Samples\\Northwind.mdb"; string strSQL = "SELECT TOP 10 ProductName, UnitPrice FROM Products " + "ORDER BY UnitPrice DESC";
try { // Open the connection cnn = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strMDB);
// if your database is secured with user-level security, // use a connection string like this: // string strConnection = // "Provider=Microsoft.Jet.OLEDB.4.0;" + // "Data Source=" strMDB + // "User ID=TheUserName;" + // "Password=ThePassword" ;
cnn.Open();
// Open the Command and DataReader. cmd = new OleDbCommand(strSQL, cnn); sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
} catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (sdr != null ) { sdr.Close(); } }
// C# // Start on second row. int intRow = 2; Object beforeRow = Type.Missing;
// Retrieve the data and insert into new rows. while (sdr.Read()) { tbl.Rows.Add(ref beforeRow); tbl.Cell(intRow, 1).Range.Text = sdr[0].ToString(); tbl.Cell(intRow, 2).Range.Text = String.Format("{0:c}", sdr[1]); intRow += 1; } // Bold the column heads. tbl.Rows[1].Range.Bold = 1;
|
No responses found. Be the first to respond and make money from revenue sharing program.
|