|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System.Dynamic; |
| 4 | + |
| 5 | +using (FileStream inputFileStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 6 | +{ |
| 7 | + // Open the input Word document |
| 8 | + using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx)) |
| 9 | + { |
| 10 | + // Find a table by the Title property. |
| 11 | + WTable table = document.FindItemByProperty(EntityType.Table, "Title", "DataTable") as WTable; |
| 12 | + // Check if the table was found. |
| 13 | + if (table != null) |
| 14 | + { |
| 15 | + // Get the second row of the table. |
| 16 | + WTableRow secondRow = table.Rows[1]; |
| 17 | + // Insert data into the cells of the second row. |
| 18 | + InsertDataToCells(secondRow); |
| 19 | + // Add dynamic rows starting at index 2, based on the second row. |
| 20 | + AddDynamicRows(table, 2, secondRow); |
| 21 | + } |
| 22 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write)) |
| 23 | + { |
| 24 | + // Save the modified document to the output file stream. |
| 25 | + document.Save(outputFileStream, FormatType.Docx); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// <summary> |
| 31 | +/// Insert data into the cells of a specified table row. |
| 32 | +/// </summary> |
| 33 | +void InsertDataToCells(WTableRow row) |
| 34 | +{ |
| 35 | + // List of placeholder data to insert into the cells. |
| 36 | + List<string> data = new List<string> { "<<Data1>>", "<<Data2>>", "<<Data3>>", "<<Data4>>" }; |
| 37 | + int count = 0; |
| 38 | + // Iterate through each cell in the specified row. |
| 39 | + foreach (WTableCell cell in row.Cells) |
| 40 | + { |
| 41 | + // Assign data to the particular cell. |
| 42 | + cell.Paragraphs[0].Text = data[count]; |
| 43 | + count++; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// <summary> |
| 48 | +/// Add dynamic rows to a specified table at a certain index. |
| 49 | +/// </summary> |
| 50 | +void AddDynamicRows(WTable table, int index, WTableRow row) |
| 51 | +{ |
| 52 | + // Create a list of dynamic row details. |
| 53 | + IEnumerable<dynamic> rowsDetails = CreateDynamicRows(); |
| 54 | + // Iterate through each dynamic row detail. |
| 55 | + foreach (dynamic rowDetails in rowsDetails) |
| 56 | + { |
| 57 | + // Retrieve cell content for the new row. |
| 58 | + List<string> cellDetails = GetListOfCellValue(rowDetails); |
| 59 | + // Clone the second row to create a new row. |
| 60 | + WTableRow newRow = row.Clone(); |
| 61 | + // Iterate through the cells of the cloned row. |
| 62 | + for (int i = 0; i < newRow.Cells.Count; i++) |
| 63 | + { |
| 64 | + // Get the cell at specific from the cloned row. |
| 65 | + WTableCell wTableCell = newRow.Cells[i]; |
| 66 | + // Modify the paragraph text of the cell with the corresponding cell detail. |
| 67 | + wTableCell.Paragraphs[0].Text = cellDetails[i]; |
| 68 | + } |
| 69 | + // Insert the newly created row at the specified index. |
| 70 | + table.Rows.Insert(index, newRow); |
| 71 | + // Increment the index for the next dynamic row. |
| 72 | + index++; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// <summary> |
| 77 | +/// Create dynamic rows with sample cell data. |
| 78 | +/// </summary> |
| 79 | +IEnumerable<dynamic> CreateDynamicRows() |
| 80 | +{ |
| 81 | + // Create a list of dynamic row details. |
| 82 | + List<dynamic> rowDetails = new List<dynamic>(); |
| 83 | + // Add dynamic cells to the row details list. |
| 84 | + rowDetails.Add(CreateDynamicCells("<<Data5>>", "<<Data6>>", "<<Data7>>", "<<Data8>>")); |
| 85 | + rowDetails.Add(CreateDynamicCells("<<Data9>>", "<<Data10>>", "<<Data11>>", "<<Data12>>")); |
| 86 | + rowDetails.Add(CreateDynamicCells("<<Data13>>", "<<Data14>>", "<<Data15>>", "<<Data16>>")); |
| 87 | + rowDetails.Add(CreateDynamicCells("<<Data17>>", "<<Data18>>", "<<Data19>>", "<<Data20>>")); |
| 88 | + // Return the list of dynamic row details. |
| 89 | + return rowDetails; |
| 90 | +} |
| 91 | + |
| 92 | +/// <summary> |
| 93 | +/// Create dynamic cell data. |
| 94 | +/// </summary> |
| 95 | +dynamic CreateDynamicCells(string cell1, string cell2, string cell3, string cell4) |
| 96 | +{ |
| 97 | + // Create a new ExpandoObject for dynamic properties. |
| 98 | + dynamic dynamicOrder = new ExpandoObject(); |
| 99 | + // Assign values to the dynamic object properties. |
| 100 | + dynamicOrder.Cell1 = cell1; |
| 101 | + dynamicOrder.Cell2 = cell2; |
| 102 | + dynamicOrder.Cell3 = cell3; |
| 103 | + dynamicOrder.Cell4 = cell4; |
| 104 | + // Return the dynamic object. |
| 105 | + return dynamicOrder; |
| 106 | +} |
| 107 | + |
| 108 | +/// <summary> |
| 109 | +/// Get the list of cell values. |
| 110 | +/// </summary> |
| 111 | +List<string> GetListOfCellValue(dynamic rowDetails) |
| 112 | +{ |
| 113 | + List<string> cellDetails = new List<string>(); |
| 114 | + // Add each dynamic cell value to the list. |
| 115 | + cellDetails.Add(rowDetails.Cell1); |
| 116 | + cellDetails.Add(rowDetails.Cell2); |
| 117 | + cellDetails.Add(rowDetails.Cell3); |
| 118 | + cellDetails.Add(rowDetails.Cell4); |
| 119 | + // Return the list of cell details. |
| 120 | + return cellDetails; |
| 121 | +} |
0 commit comments