Exemplo n.º 1
0
 private void FillTable(IEnumerable <TemplateTable> tables)
 {
     if (tables == null || !tables.Any())
     {
         return;
     }
     for (int t = 1; t <= Document.Tables.Count; t++)
     {
         Table table = Document.Tables[t];
         int   rc    = table.Rows.Count;
         if (rc > 1)
         {
             int      cc     = table.Columns.Count;
             string[] header = new string[cc];
             for (int i = 1; i <= cc; i++)
             {
                 try
                 {
                     header[i - 1] = table.Cell(1, i).Range.Text?.Trim(GetControls());
                 }
                 catch (Exception) { }
             }
             foreach (TemplateTable tt in tables)
             {
                 if (tt.IsEqual(header) && tt.GetCells() is Templates.Cell[][] cells)
                 {
                     int ri = rc;
                     for (int r = 0; r < cells.Length; r++)
                     {
                         if (cells[r] != null)
                         {
                             if (r < cells.Length - 1)
                             {
                                 table.Rows.Add();
                             }
                             FormatRow(cells[r], true);
                             for (int i = 0; i < cells[r].Length; i++)
                             {
                                 Templates.Cell cell = cells[r][i];
                                 if (cell != null && cell.Column > -1 && cell.Column < cc)
                                 {
                                     int c  = cell.Column + 1;
                                     int mc = cell.LastMergeColumn + 1;
                                     if (mc > cc)
                                     {
                                         mc = cc;
                                     }
                                     if (mc > c)
                                     {
                                         for (int j = c + 1; j <= mc; j++)
                                         {
                                             try
                                             {
                                                 table.Cell(ri, j).Range.Text = null;
                                             }
                                             catch (Exception) { }
                                         }
                                         try
                                         {
                                             table.Cell(ri, c).Merge(table.Cell(ri, mc));
                                         }
                                         catch (Exception) { }
                                     }
                                     try
                                     {
                                         table.Cell(ri, c).Range.Text = cell.Value;
                                     }
                                     catch (Exception) { }
                                 }
                             }
                             ri++;
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 private static void FillTable(IEnumerable <Table> docTables, IEnumerable <TemplateTable> tables)
 {
     if (docTables == null || tables == null || !docTables.Any() || !tables.Any())
     {
         return;
     }
     foreach (Table table in docTables)
     {
         int rc = table.Elements <TableRow>().Count();
         if (rc > 1)
         {
             string[] header = table.Elements <TableRow>().First().Elements <TableCell>().Select(x => x.InnerText?.Trim(GetControls())).ToArray();
             int      cc     = header.Length;
             foreach (TemplateTable tt in tables)
             {
                 if (tt.IsEqual(header) && tt.GetCells() is Templates.Cell[][] cells)
                 {
                     for (int r = 0; r < cells.Length; r++)
                     {
                         if (cells[r] != null)
                         {
                             TableRow    nr = table.Elements <TableRow>().Last().Clone() as TableRow;
                             TableCell[] tc = table.Elements <TableRow>().Last().Elements <TableCell>().ToArray();
                             FormatRow(cells[r], false);
                             for (int i = 0; i < cells[r].Length; i++)
                             {
                                 Templates.Cell cell = cells[r][i];
                                 if (cell != null && cell.Column > -1 && cell.Column < cc)
                                 {
                                     int c  = cell.Column;
                                     int mc = cell.LastMergeColumn;
                                     if (mc > cc - 1)
                                     {
                                         mc = cc - 1;
                                     }
                                     if (mc > c)
                                     {
                                         tc[c].Append(new TableCellProperties(new HorizontalMerge()
                                         {
                                             Val = MergedCellValues.Restart
                                         }));
                                         for (int p = c + 1; p <= mc; p++)
                                         {
                                             tc[p].Append(new TableCellProperties(new HorizontalMerge()
                                             {
                                                 Val = MergedCellValues.Continue
                                             }));
                                         }
                                     }
                                     ReplaceParagraph(tc[c], cell.Value);
                                 }
                             }
                             if (r < cells.Length - 1)
                             {
                                 table.AppendChild(nr);
                             }
                         }
                     }
                 }
             }
         }
     }
 }