Hi, I am trying to create more than one table to the word document. Actually, these tables are combined int one table. I need separated tables. Below are my codes.
Thanks,
Chi Ming
private void CreateWordDocument() { string documentName = @"C:\Users\559463\Desktop\TestWordDOMDoc.docx";
Document doc = new Document();
Document wordDocument = new Document();
Infragistics.Documents.Word.Font font = wordDocument.CreateFont(); font.Name = "Times New Roman"; font.Size = 10;
Paragraph para1 = wordDocument.ContentBlocks.AddParagraph(); para1.ContentRuns.AddTextRun(""); wordDocument.Sections.Add(para1);
wordDocument.FinalSection.Properties.PageMargins = new Padding(55, 55, 55, 55);
Paragraph footerParagraph = wordDocument.FinalSection.FooterAllPages.ContentBlocks.AddParagraph(); footerParagraph.Properties.Alignment = ParagraphAlignment.Center; footerParagraph.ContentRuns.AddTextRun(DateTime.Now.ToLongDateString(), font); footerParagraph.ContentRuns.AddTextRun(" - ", font); footerParagraph.ContentRuns.AddTextRun(DateTime.Now.ToShortDateString(), font);
footerParagraph = wordDocument.FinalSection.FooterAllPages.ContentBlocks.AddParagraph(); footerParagraph.Properties.Alignment = ParagraphAlignment.Center; footerParagraph.ContentRuns.AddTextRun("Page ", font); footerParagraph.ContentRuns.AddPageNumberField(PageNumberFieldFormat.Decimal); footerParagraph.ContentRuns.AddTextRun(" of ", font); footerParagraph.ContentRuns.AddTextRun("NumPages", font);
Infragistics.Documents.Word.Table tb1 = null; Infragistics.Documents.Word.TableRow tbrow = null; Infragistics.Documents.Word.TableCellsCollection tbcell = null; Infragistics.Documents.Word.TextRun itrInvestmentHeader = null;
tb1 = wordDocument.ContentBlocks.AddTable(); ResetCells(tb1, 1, 3); tb1.Rows[0].Cells[0].Properties.PreferredWidth = (float)502.0 * (float)0.35; tb1.Rows[0].Cells[1].Properties.PreferredWidth = (float)502.0 * (float)0.15; tb1.Rows[0].Cells[2].Properties.PreferredWidth = (float)502.0 * (float)0.5; tb1.Properties.CellMargins = new Padding(4, 4, 4, 4); tb1.Properties.BorderProperties.Style = Infragistics.Documents.Word.TableBorderStyle.None;
tbrow = tb1.Rows.Add(); AddCellsToRow(tbrow, 3);
itrInvestmentHeader = tbrow.Cells[0].ContentBlocks.AddParagraph().ContentRuns.AddTextRun("Investment Name"); itrInvestmentHeader.Font.Bold = true;
tbrow.Cells[0].Properties.VerticalMerge = TableCellVerticalMerge.None; tbrow.Cells[0].Properties.BackColor = Color.Red; itrInvestmentHeader.Font.Size = 11; itrInvestmentHeader.Font.Name = "Verdana";
wordDocument.Save(documentName);
}
AddCellsToRow(Infragistics.Documents.Word.TableRow row, int cols)
{ for (int i = 0; i < cols; i++) { row.Cells.Add(); } }
private void ResetCells(Infragistics.Documents.Word.Table table, int rowNum, int colNum) { for (int colin = 0; colin <= colnum="" -="" 1="" colin="" br=""> { table.Columns.Add(); }
for (int rowin = 0; rowin <= rownum="" -="" 1="" rowin="" br=""> { Infragistics.Documents.Word.TableRow row = table.Rows.Add(); for (int colin = 0; colin <= colnum="" -="" 1="" colin="" br=""> { row.Cells.Add(); } }
Hello Chi,
I am just checking if you require any further assistance on the matter.