// Copyright 2017 Baliance. All rights reserved. package main import ( "log" "baliance.com/gooxml/color" "baliance.com/gooxml/document" "baliance.com/gooxml/measurement" "baliance.com/gooxml/schema/soo/wml" ) func main() { doc := document.New() // First Table { table := doc.AddTable() // width of the page table.Properties().SetWidthPercent(100) // with thick borers borders := table.Properties().Borders() borders.SetAll(wml.ST_BorderSingle, color.Auto, 2*measurement.Point) row := table.AddRow() run := row.AddCell().AddParagraph().AddRun() run.AddText("Name") run.Properties().SetHighlight(wml.ST_HighlightColorYellow) row.AddCell().AddParagraph().AddRun().AddText("John Smith") row = table.AddRow() row.AddCell().AddParagraph().AddRun().AddText("Street Address") row.AddCell().AddParagraph().AddRun().AddText("111 Country Road") } doc.AddParagraph() // break up the consecutive tables // Second Table { table := doc.AddTable() // 4 inches wide table.Properties().SetWidth(4 * measurement.Inch) borders := table.Properties().Borders() // thin borders borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero) row := table.AddRow() cell := row.AddCell() // column span cell.Properties().SetColumnSpan(2) run := cell.AddParagraph().AddRun() run.AddText("Cells can span multiple columns") row = table.AddRow() row.AddCell().AddParagraph().AddRun().AddText("Street Address") row.AddCell().AddParagraph().AddRun().AddText("111 Country Road") } doc.AddParagraph() // Third Table { table := doc.AddTable() table.Properties().SetWidthPercent(100) borders := table.Properties().Borders() borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point) hdrRow := table.AddRow() cell := hdrRow.AddCell() cell.Properties().SetShading(wml.ST_ShdSolid, color.LightGray, color.Auto) cellPara := cell.AddParagraph() cellPara.Properties().SetAlignment(wml.ST_JcLeft) cellPara.AddRun().AddText("Left Align") cell = hdrRow.AddCell() cell.Properties().SetShading(wml.ST_ShdThinDiagStripe, color.Red, color.LightGray) cellPara = cell.AddParagraph() cellPara.Properties().SetAlignment(wml.ST_JcCenter) cellPara.AddRun().AddText("Center Align") cell = hdrRow.AddCell() cell.Properties().SetShading(wml.ST_ShdPct20, color.Red, color.LightGray) cellPara = cell.AddParagraph() cellPara.Properties().SetAlignment(wml.ST_JcRight) cellPara.AddRun().AddText("Right Align") veryLightGray := color.RGB(240, 240, 240) for i := 0; i < 5; i++ { row := table.AddRow() for j := 0; j < 3; j++ { cell := row.AddCell() // shade every other row if i%2 == 0 { cell.Properties().SetShading(wml.ST_ShdSolid, veryLightGray, color.Auto) } cell.AddParagraph().AddRun() } } } doc.AddParagraph() // Fourth Table { table := doc.AddTable() table.Properties().SetWidthPercent(50) table.Properties().SetAlignment(wml.ST_JcTableCenter) borders := table.Properties().Borders() borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point) row := table.AddRow() row.Properties().SetHeight(2*measurement.Inch, wml.ST_HeightRuleExact) cell := row.AddCell() cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter) para := cell.AddParagraph() para.Properties().SetAlignment(wml.ST_JcCenter) run := para.AddRun() run.AddText("hello world") } doc.AddParagraph() // Fifth Table { table := doc.AddTable() table.Properties().SetWidthPercent(90) table.Properties().SetAlignment(wml.ST_JcTableCenter) borders := table.Properties().Borders() borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point) row := table.AddRow() cell := row.AddCell() cell.Properties().SetWidthPercent(25) para := cell.AddParagraph() run := para.AddRun() run.AddText("hello") cell = row.AddCell() cell.Properties().SetWidthPercent(75) para = cell.AddParagraph() run = para.AddRun() run.AddText("world") // start a new table doc.AddParagraph() table = doc.AddTable() table.Properties().SetWidthPercent(90) table.Properties().SetAlignment(wml.ST_JcTableCenter) borders = table.Properties().Borders() borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point) row = table.AddRow() cell = row.AddCell() cell.Properties().SetWidth(0.25 * measurement.Inch) para = cell.AddParagraph() run = para.AddRun() run.AddText("hello") cell = row.AddCell() cell.Properties().SetWidth(2.5 * measurement.Inch) para = cell.AddParagraph() run = para.AddRun() run.AddText("world") } if err := doc.Validate(); err != nil { log.Fatalf("error during validation: %s", err) } doc.SaveToFile("tables.docx") }