NestedTable.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Word
  8.  
  9. '' This sample creates a simple table and adds another (nested)
  10. '' table into a cell of the first table.
  11. Public Class NestedTable
  12. Function CreateDocx() As GcWordDocument
  13. Dim doc = New GcWordDocument()
  14.  
  15. doc.Body.Paragraphs.Add("Here is a 5 columns by 3 rows table, with a nested table in cell (2, 2):")
  16. Dim t = doc.Body.Tables.Add(
  17. {
  18. New String() {"row 1, col 1", "row 1, col 2", "row 1, col 3", "row 1, col 4", "row 1, col 5"},
  19. New String() {"row 2, col 1", "row 2, col 2", "row 2, col 3", "row 2, col 4", "row 2, col 5"},
  20. New String() {"row 3, col 1", "row 3, col 2", "row 3, col 3", "row 3, col 4", "row 3, col 5"}
  21. }
  22. )
  23. '' Create a New table style
  24. Dim ts1 = doc.Styles.Add("Table Style 1", StyleType.Table)
  25. '' We can enumerate all table borders, including inside borders:
  26. For Each border In ts1.Table.Borders
  27. border.LineStyle = LineStyle.Triple
  28. border.LineWidth = 0.5F
  29. border.Color.RGB = Color.Purple
  30. Next
  31. '' Assign the style to the table:
  32. t.Style = ts1
  33.  
  34. '' Overwrite inside border's line styles:
  35. ts1.Table.Borders.InsideHorizontal.LineStyle = LineStyle.Double
  36. ts1.Table.Borders.InsideVertical.LineStyle = LineStyle.Double
  37.  
  38. '' Add some padding:
  39. ts1.Table.Padding.All = 3
  40.  
  41. '' Set up cell (1,1) to visually span two following cells
  42. Dim cell = t.Rows(1).Cells(1)
  43. Dim tn = cell.GetRange().Tables.Add()
  44. cell.GetRange().Paragraphs.First.GetRange().Texts.First.Value = "Cell with nested table."
  45.  
  46. tn.Rows.Add("Nested (1,1)", "Nested (1,2)", "Nested (1,3)")
  47. tn.Rows.Add("Nested (2,1)", "Nested (2,2)", "Nested (2,3)")
  48. tn.Rows.Add("Nested (3,1)", "Nested (3,2)", "Nested (3,3)")
  49.  
  50. '' Done
  51. Return doc
  52. End Function
  53. End Class
  54.