public void ParentTypeConstraintTest() { Word.TableProperties tblPr = new Word.TableProperties(); ValidationContext context = new ValidationContext() { Element = tblPr }; ParentTypeConstraint constraint = new ParentTypeConstraint() { ParentType = typeof(Word.Table) }; Assert.Null(constraint.Validate(context)); Word.Table tbl = new Word.Table(); tbl.Append(tblPr); Assert.Null(constraint.Validate(context)); tbl.RemoveAllChildren(); Word.Paragraph p = new Word.Paragraph(); p.Append(tblPr); Assert.NotNull(constraint.Validate(context)); }
public Table CreateTable(int cols) { // TODO : Fix the bug, Applying a style to a table is not working Table table = new Table(); TableProperties tableProperties = new TableProperties( new TableStyle() { Val = DocStyles.TableBlue.ToDescriptionString() }, new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct }); table.AppendChild <TableProperties>(tableProperties); // May be useless, or for performances reasons TableGrid tg = new TableGrid(); for (int i = 0; i < cols; i++) { tg.Append(new GridColumn()); } table.AppendChild(tg); _body.Append(table); return(table); }
/// <summary> /// Creates a new Table from the supplied input data. /// </summary> /// <param name="json"> /// String in JSON format representing the tabular data for updating the Chart's cached data points. /// The JSON object must contain a "fields" attribute as an array containing the field/column names. /// The JSON object must contain a "rows" attribute as an array of arrays representing the rows and their values, with values matching the same order and cardinality of the field names. /// This is the same data as the underlying Excel spreadsheet contents.</param> /// <param name="tableStyle"> /// String containing the name of the Wordprocessing.TableStyle to apply to the table.</param> /// <returns> /// Returns a new Wordprocessing.Table containing the tabular data from the JSON input, formatted with the specified TableStyle.</returns> public static wp.Table BuildTable(string json, string tableStyle) { json = ((json == String.Empty) || (json == null)) ? "{\"fields\": [ \"No Results\" ], \"rows\": [[ \"No Results\" ]]}" : json; //Splunk JSON data is a series of objects consisting of multiple key(column)/value(row) pairs in the result attribute. dynamic input = JsonConvert.DeserializeObject <dynamic>(json); if (input["rows"].Count == 0) { json = "{\"fields\": [ \"No Results\" ], \"rows\": [[ \"No Results\" ]]}"; input = JsonConvert.DeserializeObject <dynamic>(json); } wp.Table result = new wp.Table(); wp.TableProperties tableProperties1 = new wp.TableProperties(); wp.TableStyle tableStyle1 = new wp.TableStyle() { Val = tableStyle }; wp.TableWidth tableWidth1 = new wp.TableWidth() { Width = "5000", Type = wp.TableWidthUnitValues.Pct }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); result.Append(tableProperties1); wp.TableGrid tableGrid = new wp.TableGrid(); //Build table header row wp.TableRow headerRow = new wp.TableRow(); foreach (var columnName in input["fields"]) { headerRow.Append(new wp.TableCell(new wp.Paragraph(new wp.Run(new wp.Text(columnName.ToString()))))); tableGrid.Append(new wp.GridColumn()); } result.Append(tableGrid); result.Append(headerRow); //Build table data rows foreach (var row in input["rows"]) { wp.TableRow tr = new wp.TableRow(); foreach (var cell in row) { tr.Append(new wp.TableCell(new wp.Paragraph(new wp.Run(new wp.Text(cell.ToString()))))); } result.Append(tr); } return(result); }
public TableProperties GetTableProperties() { var result = new TableProperties(); if (Width != null) { result.AppendChild(Width.ToTableWidthType <TableWidth>()); } result.TableCellMarginDefault = new TableCellMarginDefault(); if (CellLeftMargin != null) { result.TableCellMarginDefault.TableCellLeftMargin = CellLeftMargin.ToTableMargin <TableCellLeftMargin>(); } if (CellTopMargin != null) { result.TableCellMarginDefault.TopMargin = CellTopMargin.ToTableWidthType <TopMargin>(); } if (CellRightMargin != null) { result.TableCellMarginDefault.TableCellRightMargin = CellRightMargin.ToTableMargin <TableCellRightMargin>(); } if (CellBottomMargin != null) { result.TableCellMarginDefault.BottomMargin = CellBottomMargin.ToTableWidthType <BottomMargin>(); } if (CellSpacing != null) { result.TableCellSpacing = new TableCellSpacing { Width = CellSpacing }; } result.TableBorders = Borders; return(result); }
public void SetTableProperties() { using (var doc = WordprocessingDocument.Open(TemplatePath, true, new OpenSettings { AutoSave = false })) { var table = new WordDocumentTable(doc); table.Select("Table1"); var ntp = new TableProperties( new TableCaption { Val = "Table2" }); table.TableProperties = ntp; var tp = (TableProperties)table.TableProperties; Assert.IsNotNull(tp); Assert.AreEqual(tp.TableCaption.Val.ToString(), "Table2"); } }
//:::::WORD DOCUMENT PROCESSING PART::::: public static void AddToTable(String fileName, String tableName, int elemNo, String txt, int elemRow = 0) { List <WordP.Table> tables = null; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true)) { tables = new List <WordP.Table>(wordDoc.MainDocumentPart.Document.Body.Elements <WordP.Table>()); tables.ForEach(table => { WordP.TableProperties tblProperties = table.GetFirstChild <WordP.TableProperties>(); if (tblProperties != null) { WordP.TableCaption caption = tblProperties.GetFirstChild <WordP.TableCaption>(); if (caption != null) { if (caption.Val.HasValue && caption.Val.Value.Equals(tableName)) { WordP.TableRow row = table.Elements <WordP.TableRow>().ElementAt(elemNo); // Find the third cell in the row. WordP.TableCell cell = row.Elements <WordP.TableCell>().ElementAt(elemRow); // Find the first paragraph in the table cell. WordP.Paragraph p = cell.Elements <WordP.Paragraph>().First(); // Find the first run in the paragraph. WordP.Run r = p.Elements <WordP.Run>().First(); // Set the text for the run. WordP.Text t = r.Elements <WordP.Text>().First(); t.Text = txt; // you have got your table. process the rest. } } } }); } }
/// <summary> /// The write table. /// </summary> /// <param name="t">The t.</param> public void WriteTable(Table t) { this.body.AppendChild(CreateParagraph(t.GetFullCaption(this.style), TableCaptionId)); var table = new DocumentFormat.OpenXml.Wordprocessing.Table(); var tableProperties1 = new TableProperties(); var tableStyle1 = new TableStyle { Val = "TableGrid" }; var tableWidth1 = new TableWidth { Width = "0", Type = TableWidthUnitValues.Auto }; var tableLook1 = new TableLook { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties1.AppendChild(tableStyle1); tableProperties1.AppendChild(tableWidth1); tableProperties1.AppendChild(tableLook1); var tableGrid1 = new TableGrid(); // ReSharper disable once UnusedVariable foreach (var tc in t.Columns) { // TODO: use tc.Width to set the width of the column var gridColumn1 = new GridColumn { Width = "3070" }; tableGrid1.AppendChild(gridColumn1); } foreach (var row in t.Rows) { var tr = new TableRow(); if (row.IsHeader) { var trp = new TableRowProperties(); var tableHeader1 = new TableHeader(); trp.AppendChild(tableHeader1); tr.AppendChild(trp); } int j = 0; foreach (var c in row.Cells) { bool isHeader = row.IsHeader || t.Columns[j++].IsHeader; var cell = new TableCell(); var tcp = new TableCellProperties(); var borders = new TableCellBorders(); borders.AppendChild( new BottomBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); borders.AppendChild( new TopBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); borders.AppendChild( new LeftBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); borders.AppendChild( new RightBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); tcp.AppendChild(borders); cell.AppendChild(tcp); string styleId = isHeader ? "TableHeader" : "TableText"; cell.AppendChild(CreateParagraph(c.Content, styleId)); tr.AppendChild(cell); } table.AppendChild(tr); } this.body.AppendChild(table); }
// Insert a table into a word processing document. public static void CreateTable(string fileName) { // Use the file name and path passed in as an argument // to open an existing Word 2007 document. using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true)) { // Create an empty table. DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table(); // Create a TableProperties object and specify its border information. DocumentFormat.OpenXml.Wordprocessing.TableProperties tblProp = new DocumentFormat.OpenXml.Wordprocessing.TableProperties( new TableBorders( new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = new EnumValue <BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = new EnumValue <BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = new EnumValue <BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = new EnumValue <BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.InsideHorizontalBorder() { Val = new EnumValue <BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.InsideVerticalBorder() { Val = new EnumValue <BorderValues>(BorderValues.Dashed), Size = 24 } ) ); // Append the TableProperties object to the empty table. table.AppendChild <DocumentFormat.OpenXml.Wordprocessing.TableProperties>(tblProp); // Create a row. DocumentFormat.OpenXml.Wordprocessing.TableRow tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow(); // Create a cell. DocumentFormat.OpenXml.Wordprocessing.TableCell tc1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(); // Specify the width property of the table cell. tc1.Append(new DocumentFormat.OpenXml.Wordprocessing.TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })); // Specify the table cell content. tc1.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text("some text")))); // Append the table cell to the table row. tr.Append(tc1); // Create a second table cell by copying the OuterXml value of the first table cell. DocumentFormat.OpenXml.Wordprocessing.TableCell tc2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(tc1.OuterXml); // Append the table cell to the table row. tr.Append(tc2); // Append the table row to the table. table.Append(tr); // Append the table to the document. doc.MainDocumentPart.Document.Body.Append(table); } }
// Insert a table into a word processing document. public static void CreateTable(string fileName) { // Use the file name and path passed in as an argument // to open an existing Word 2007 document. using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true)) { // Create an empty table. DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table(); // Create a TableProperties object and specify its border information. DocumentFormat.OpenXml.Wordprocessing.TableProperties tblProp = new DocumentFormat.OpenXml.Wordprocessing.TableProperties( new TableBorders( new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, new DocumentFormat.OpenXml.Wordprocessing.InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 } ) ); // Append the TableProperties object to the empty table. table.AppendChild<DocumentFormat.OpenXml.Wordprocessing.TableProperties>(tblProp); // Create a row. DocumentFormat.OpenXml.Wordprocessing.TableRow tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow(); // Create a cell. DocumentFormat.OpenXml.Wordprocessing.TableCell tc1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(); // Specify the width property of the table cell. tc1.Append(new DocumentFormat.OpenXml.Wordprocessing.TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })); // Specify the table cell content. tc1.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text("some text")))); // Append the table cell to the table row. tr.Append(tc1); // Create a second table cell by copying the OuterXml value of the first table cell. DocumentFormat.OpenXml.Wordprocessing.TableCell tc2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(tc1.OuterXml); // Append the table cell to the table row. tr.Append(tc2); // Append the table row to the table. table.Append(tr); // Append the table to the document. doc.MainDocumentPart.Document.Body.Append(table); } }
/// <summary> /// Get the nearest applied element (e.g. RunFonts, Languages) in the style hierachy. It searches upstream from current $obj til reach the top of the hierachy if no found. /// </summary> /// <typeparam name="T">Target element type (e.g. RunFonts.GetType()).</typeparam> /// <param name="obj">The OpenXmlElemet to search from.</param> /// <returns>Return found element or null if not found.</returns> public T GetAppliedElement <T>(OpenXmlElement obj) { T ret = default(T); if (obj == null) { return(ret); } Type objType = obj.GetType(); if (objType == typeof(Word.Run)) { // Run.RunProperties > Run.RunProperties.rStyle > Paragraph.ParagraphProperties.pStyle (> default style > docDefaults) // ( ): done in paragraph level Word.RunProperties runpr = StyleHelper.GetElement <Word.RunProperties>(obj); if (runpr != null) { ret = StyleHelper.GetDescendants <T>(runpr); // If has rStyle, go through rStyle before go on. // Use getAppliedStyleElement() is because it will go over all the basedOn styles. if (ret == null && runpr.RunStyle != null) { ret = this.GetAppliedElement <T>(this.GetStyleById(runpr.RunStyle.Val)); } } if (ret == null) { // parent paragraph's pStyle if (obj.Parent != null && obj.Parent.GetType() == typeof(Word.Paragraph)) { Word.Paragraph pg = obj.Parent as Word.Paragraph; if (pg.ParagraphProperties != null && pg.ParagraphProperties.ParagraphStyleId != null) { ret = this.GetAppliedElement <T>(this.GetStyleById(pg.ParagraphProperties.ParagraphStyleId.Val)); } } } if (ret == null) // default run style { ret = this.GetAppliedElement <T>(this.GetDefaultStyle(DefaultStyleType.Character)); } if (ret == null) // docDefaults { ret = StyleHelper.GetDescendants <T>(this.docDefaults.RunPropertiesDefault); } } else if (objType == typeof(Word.Paragraph)) { // Paragraph.ParagraphProperties > Paragraph.ParagraphProperties.pStyle > default style > docDefaults Word.ParagraphProperties pgpr = StyleHelper.GetElement <Word.ParagraphProperties>(obj); if (pgpr != null) { ret = StyleHelper.GetDescendants <T>(pgpr); // If has pStyle, go through pStyle before go on. // Use getAppliedStyleElement() is because it will go over the whole Style hierachy. if (ret == null && pgpr.ParagraphStyleId != null) { ret = this.GetAppliedElement <T>(this.GetStyleById(pgpr.ParagraphStyleId.Val)); } } if (ret == null) { if (obj.Parent != null && obj.Parent.GetType() == typeof(Word.TableCell)) { for (int i = 0; i < 3 && obj != null; i++) { obj = (obj.Parent != null) ? obj.Parent : null; } if (obj != null && obj.GetType() == typeof(Word.Table)) { ret = this.GetAppliedElement <T>(obj); } } } if (ret == null) // default paragraph style { ret = this.GetAppliedElement <T>(this.GetDefaultStyle(DefaultStyleType.Paragraph)); } if (ret == null) // docDefaults { ret = StyleHelper.GetDescendants <T>(this.docDefaults); } } else if (objType == typeof(Word.Table)) { // Table.TableProperties > Table.TableProperties.tblStyle > default style Word.TableProperties tblpr = StyleHelper.GetElement <Word.TableProperties>(obj); if (tblpr != null) { ret = StyleHelper.GetDescendants <T>(tblpr); // If has tblStyle, go through tblStyle before go on. // Use getAppliedStyleElement() is because it will go over the whole Style hierachy. if (ret == null && tblpr.TableStyle != null) { ret = this.GetAppliedElement <T>(this.GetStyleById(tblpr.TableStyle.Val)); } } if (ret == null) // default table style { ret = this.GetAppliedElement <T>(this.GetDefaultStyle(DefaultStyleType.Table)); } } else if (objType == typeof(Word.TableRow)) { // TableRow.TableRowProperties > Table.TableProperties.tblStyle (> default style) // ( ): done in Table level Word.TableRowProperties rowpr = StyleHelper.GetElement <Word.TableRowProperties>(obj); if (rowpr != null) { ret = StyleHelper.GetDescendants <T>(rowpr); } if (ret == null) { for (int i = 0; i < 1 && obj != null; i++) { obj = (obj.Parent != null) ? obj.Parent : null; } if (obj != null && obj.GetType() == typeof(Word.Table)) { ret = this.GetAppliedElement <T>(obj); } } } else if (objType == typeof(Word.TableCell)) { // TableCell.TableCellProperties > Table.TableProperties.tblStyle (> default style) // ( ): done in Table level Word.TableCellProperties cellpr = StyleHelper.GetElement <Word.TableCellProperties>(obj); if (cellpr != null) { ret = StyleHelper.GetDescendants <T>(cellpr); } if (ret == null) { for (int i = 0; i < 2 && obj != null; i++) { obj = (obj.Parent != null) ? obj.Parent : null; } if (obj != null && obj.GetType() == typeof(Word.Table)) { ret = this.GetAppliedElement <T>(obj); } } } else if (objType == typeof(Word.Style)) { Word.Style st = obj as Word.Style; ret = StyleHelper.GetDescendants <T>(st); if (ret == null) { if (st.BasedOn != null) { ret = this.GetAppliedElement <T>(this.GetStyleById(st.BasedOn.Val)); } } } else // unknown type, just get everything can get { ret = StyleHelper.GetDescendants <T>(obj); } return(ret); }
/// <summary> /// Rolling up table border property from TableProperties > TableStyle > Default style. /// </summary> /// <param name="table"></param> /// <returns></returns> private void rollingUpTableBorders() { if (this.table == null) { return; } this.condFmtbr = new Word.TableBorders(); List <Word.TableBorders> borders = new List <Word.TableBorders>(); Word.TableProperties tblPrs = this.table.Elements <Word.TableProperties>().FirstOrDefault(); if (tblPrs != null) { // get from table properties (priority 1) Word.TableBorders tmp = tblPrs.Descendants <Word.TableBorders>().FirstOrDefault(); if (tmp != null) { borders.Insert(0, tmp); } // get from styles (priority 2) if (tblPrs.TableStyle != null && tblPrs.TableStyle.Val != null) { Word.Style st = this.styleHelper.GetStyleById(tblPrs.TableStyle.Val); while (st != null) { tmp = st.Descendants <Word.TableBorders>().FirstOrDefault(); if (tmp != null) { borders.Insert(0, tmp); } st = (st.BasedOn != null && st.BasedOn.Val != null) ? this.styleHelper.GetStyleById(st.BasedOn.Val) : null; } } // get from default table style (priority 3) Word.Style defaultTableStyle = this.styleHelper.GetDefaultStyle(StyleHelper.DefaultStyleType.Table); if (defaultTableStyle != null) { tmp = defaultTableStyle.Descendants <Word.TableBorders>().FirstOrDefault(); if (tmp != null) { borders.Insert(0, tmp); } } } foreach (Word.TableBorders border in borders) { if (border.TopBorder != null) { if (this.condFmtbr.TopBorder == null) { this.condFmtbr.TopBorder = (Word.TopBorder)border.TopBorder.CloneNode(true); } else { StyleHelper.CopyAttributes(this.condFmtbr.TopBorder, border.TopBorder); } } if (border.BottomBorder != null) { if (this.condFmtbr.BottomBorder == null) { this.condFmtbr.BottomBorder = (Word.BottomBorder)border.BottomBorder.CloneNode(true); } else { StyleHelper.CopyAttributes(this.condFmtbr.BottomBorder, border.BottomBorder); } } if (border.LeftBorder != null) { if (this.condFmtbr.LeftBorder == null) { this.condFmtbr.LeftBorder = (Word.LeftBorder)border.LeftBorder.CloneNode(true); } else { StyleHelper.CopyAttributes(this.condFmtbr.LeftBorder, border.LeftBorder); } } if (border.RightBorder != null) { if (this.condFmtbr.RightBorder == null) { this.condFmtbr.RightBorder = (Word.RightBorder)border.RightBorder.CloneNode(true); } else { StyleHelper.CopyAttributes(this.condFmtbr.RightBorder, border.RightBorder); } } if (border.InsideHorizontalBorder != null) { if (this.condFmtbr.InsideHorizontalBorder == null) { this.condFmtbr.InsideHorizontalBorder = (Word.InsideHorizontalBorder)border.InsideHorizontalBorder.CloneNode(true); } else { StyleHelper.CopyAttributes(this.condFmtbr.InsideHorizontalBorder, border.InsideHorizontalBorder); } } if (border.InsideVerticalBorder != null) { if (this.condFmtbr.InsideVerticalBorder == null) { this.condFmtbr.InsideVerticalBorder = (Word.InsideVerticalBorder)border.InsideVerticalBorder.CloneNode(true); } else { StyleHelper.CopyAttributes(this.condFmtbr.InsideVerticalBorder, border.InsideVerticalBorder); } } } }
void ImportProjectsAndMilestones(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile spreadsheetFileName) { ArrayList cellText = new ArrayList(); // Create a Word table. Word.Table tbl = new Word.Table(); Word.TableProperties tblPr = new Word.TableProperties(); Word.TableStyle tblStyle = new Word.TableStyle(); tblStyle.Val = "LightShading-Accent1"; tblPr.AppendChild(tblStyle); Word.TableWidth tblW = new Word.TableWidth(); tblW.Width = "5000"; tblW.Type = Word.TableWidthUnitValues.Pct; tblPr.Append(tblW); tbl.AppendChild(tblPr); byte[] byteArray = spreadsheetFileName.OpenBinary(); using (MemoryStream mem = new MemoryStream()) { mem.Write(byteArray, 0, (int)byteArray.Length); using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true)) { WorkbookPart workbookPart = mySpreadsheet.WorkbookPart; WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet, "Sheet1"); Excel.SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<Excel.SheetData>(); foreach (Excel.Row r in sheetData) { foreach (Excel.Cell c in r) { cellText.Add(XLGetCellValue(c, workbookPart)); } Word.TableRow tr = CreateRow(cellText); tbl.Append(tr); cellText = new ArrayList(); } } } // Swap out the content control for the SmartArt. OpenXmlElement parent = sdt.Parent; parent.InsertAfter(tbl, sdt); sdt.Remove(); }
private static void FillHeaderTableProperties(OpenXmlElement table) { var tableBorders = new TableBorders { TopBorder = new TopBorder { Color = "000000", Val = new EnumValue <BorderValues>(BorderValues.Single), Size = new UInt32Value((uint)10), Space = new UInt32Value((uint)0) }, LeftBorder = new LeftBorder { Color = "000000", Val = new EnumValue <BorderValues>(BorderValues.Single), Size = new UInt32Value((uint)10), Space = new UInt32Value((uint)0) }, BottomBorder = new BottomBorder { Color = "000000", Val = new EnumValue <BorderValues>(BorderValues.Single), Size = new UInt32Value((uint)10), Space = new UInt32Value((uint)0) }, RightBorder = new RightBorder { Color = "000000", Val = new EnumValue <BorderValues>(BorderValues.Single), Size = new UInt32Value((uint)10), Space = new UInt32Value((uint)0) }, InsideHorizontalBorder = new InsideHorizontalBorder { Color = "000000", Val = new EnumValue <BorderValues>(BorderValues.Single), Size = new UInt32Value((uint)10), Space = new UInt32Value((uint)0) }, InsideVerticalBorder = new InsideVerticalBorder { Color = "000000", Val = new EnumValue <BorderValues>(BorderValues.Single), Size = new UInt32Value((uint)10), Space = new UInt32Value((uint)0) } }; var tableCellMargins = new TableCellMarginDefault { TableCellLeftMargin = new TableCellLeftMargin { Width = 10, Type = new EnumValue <TableWidthValues> { Value = TableWidthValues.Dxa } }, TableCellRightMargin = new TableCellRightMargin { Width = 10, Type = new EnumValue <TableWidthValues> { Value = TableWidthValues.Dxa } } }; var tableProperties = new TableProperties { TableWidth = new TableWidth { Width = "0", Type = new EnumValue <TableWidthUnitValues> { Value = TableWidthUnitValues.Auto } }, TableIndentation = new TableIndentation { Width = new Int32Value(10), Type = new EnumValue <TableWidthUnitValues> { Value = TableWidthUnitValues.Auto } }, TableBorders = tableBorders, TableCellMarginDefault = tableCellMargins, TableLook = new TableLook { Val = HexBinaryValue.FromString("0000"), FirstRow = OnOffValue.FromBoolean(false), LastRow = OnOffValue.FromBoolean(false), FirstColumn = OnOffValue.FromBoolean(false), LastColumn = OnOffValue.FromBoolean(false), NoHorizontalBand = OnOffValue.FromBoolean(false), NoVerticalBand = OnOffValue.FromBoolean(false) } }; table.AppendChild(tableProperties); }
private static Word.TableProperties GetTableProperties(Table table) { Word.TableWidth widthProps = new Word.TableWidth(); switch (table.WidthUnits) { case ElementWidth.Absolute: widthProps.Width = (table.Width * 20).ToString(); widthProps.Type = Word.TableWidthUnitValues.Dxa; break; case ElementWidth.Percentage: widthProps.Width = (table.Width * 50).ToString(); widthProps.Type = Word.TableWidthUnitValues.Pct; break; default: break; } Word.BorderValues border; switch (table.BorderType) { case TableBorders.None: border = Word.BorderValues.None; break; case TableBorders.Single: border = Word.BorderValues.Single; break; default: border = Word.BorderValues.None; break; } Word.TableProperties tableProps = new Word.TableProperties() { TableWidth = widthProps , TableBorders = new Word.TableBorders() { LeftBorder = new Word.LeftBorder() { Val = border } , RightBorder = new Word.RightBorder() { Val = border } , TopBorder = new Word.TopBorder() { Val = border } , BottomBorder = new Word.BottomBorder() { Val = border } , InsideHorizontalBorder = new Word.InsideHorizontalBorder() { Val = border } , InsideVerticalBorder = new Word.InsideVerticalBorder() { Val = border } } }; return tableProps; }
public void InsertForcesTable(IForcesModel forces) { Paragraph para = _body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); RunProperties runProperties = getHeading2(run); run.AppendChild(new Text(++_diagmarCounter + ". Decision Forces Viewpoint: " + forces.Name)); //_body.AppendChild(new Paragraph(new Run(new Text(++_diagmarCounter + ". Decision Forces Viewpoint")))); _body.AppendChild(new Paragraph(new Run(new Text()))); var table = new Table(); var props = new TableProperties( new TableBorders( new TopBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new BottomBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new LeftBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new RightBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new InsideHorizontalBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new InsideVerticalBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 })); table.AppendChild(props); var emptyCell = new TableCell(); // insert an empty cell in tol left of forces table var decRow = new TableRow(); decRow.AppendChild(emptyCell); emptyCell.AppendChild(new Paragraph(new Run(new Text("")))); // insert the concern header and the decisions names var concCellHeader = new TableCell(new Paragraph(new Run(new Text("Concerns")))); decRow.AppendChild(concCellHeader); foreach ( TableCell decCell in forces.GetDecisions() .Select(decision => new TableCell(new Paragraph(new Run(new Text(decision.Name)))))) { decRow.AppendChild(decCell); } table.AppendChild(decRow); foreach (var concernsPerForces in forces.GetConcernsPerForce()) { IEAElement force = concernsPerForces.Key; List <IEAElement> concerns = concernsPerForces.Value; foreach (IEAElement concern in concerns) { var forceRow = new TableRow(); var forceCell = new TableCell(new Paragraph(new Run(new Text(force.Name)))); forceRow.AppendChild(forceCell); var concCell = new TableCell(); concCell.AppendChild(new Paragraph(new Run(new Text(concern.Name)))); forceRow.AppendChild(concCell); // insert ratings foreach (Rating rating in forces.GetRatings()) { if (rating.ForceGUID != force.GUID || rating.ConcernGUID != concern.GUID) { continue; } if (forces.GetDecisions().Any(decision => rating.DecisionGUID == decision.GUID)) { var ratCell = new TableCell(); ratCell.AppendChild(new Paragraph(new Run(new Text(rating.Value)))); forceRow.AppendChild(ratCell); } } table.AppendChild(forceRow); } } _body.AppendChild(table); _body.AppendChild(new Paragraph()); }
public void InsertDecisionTable(IDecision decision) { var dataDict = new Dictionary <String, IList <String> >(); dataDict.Add("Name", new List <string>()); dataDict.Add("State", new List <string>()); dataDict.Add("Problem", new List <string>()); dataDict.Add("Decision", new List <string>()); dataDict.Add("Argumentation", new List <string>()); dataDict.Add("Alternatives", new List <string>()); dataDict.Add("Related Decisions", new List <string>()); dataDict.Add("Forces", new List <string>()); dataDict.Add("Traces", new List <string>()); dataDict.Add("Stakeholder Involvement", new List <string>()); dataDict.Add("History", new List <string>()); dataDict["Name"].Add(decision.Name); dataDict["State"].Add(decision.State); //dataDict["Problem"].Add(decision.Problem); //dataDict["Decision"].Add(decision.Solution); //dataDict["Argumentation"].Add(decision.Argumentation); _decisionCounter++; //_body.AppendChild(new Paragraph(new Run(new Text("Decision " +_decisionCounter.ToString() +": " + decision.Name)))); var table = new Table(); var props = new TableProperties( new TableBorders( new TopBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new BottomBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new LeftBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new RightBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new InsideHorizontalBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new InsideVerticalBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 11 }, new TableWidth { Width = "5000", Type = TableWidthUnitValues.Pct } ), new TableCaption { Val = new StringValue("My Caption Val") // Does not work.. }); table.AppendChild(props); foreach (IDecisionRelation relation in decision.RelatedDecisions) { dataDict["Related Decisions"].Add(relation.Decision.GUID.Equals(decision.GUID) ? "<<this>> " + decision.Name + " - " + relation.Type + " - " + relation.RelatedDecision.Name : relation.Decision.Name + " - " + relation.Type + " - <<this>> " + decision.Name + "\r\n"); } foreach (IDecisionRelation alternative in decision.Alternatives) { dataDict["Alternatives"].Add(alternative.Decision.GUID.Equals(decision.GUID) ? "<<this>> " + decision.Name + " - " + alternative.Type + " - " + alternative.RelatedDecision.Name : alternative.Decision.Name + " - " + alternative.Type + " - <<this>> " + decision.Name + "\r\n"); } foreach (IForceEvaluation rating in decision.Forces) { IEAElement force = EAMain.Repository.GetElementByGUID(rating.Force.ForceGUID); dataDict["Forces"].Add(rating.Force.Name + " - " + force.Notes); } foreach (ITraceLink trace in decision.Traces) { dataDict["Traces"].Add(trace.TracedElementName); } foreach (IHistoryEntry entry in decision.History) { dataDict["History"].Add(entry.State + " " + entry.Modified.ToShortDateString()); } foreach (IStakeholderAction stakeholderInvolvment in decision.Stakeholders) { string line = string.Format(Messages.ReportingStakeholderInvolvmentLine, stakeholderInvolvment.Stakeholder.Role, stakeholderInvolvment.Stakeholder.Name, stakeholderInvolvment.Action); dataDict["Stakeholder Involvement"].Add(line); } foreach (var entry in dataDict) { if (entry.Value.Count == 0) { continue; } if ("".Equals(entry.Value[0])) { continue; } var tableRow = new TableRow(); var rowHeader = new TableCell(); rowHeader.Append(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "1821" }); rowHeader.Append( new TableCellProperties(new Shading { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "##d3d4d6" })); Paragraph para = rowHeader.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); getBold(run); run.AppendChild(new Text(entry.Key)); var rowValue = new TableCell(); Paragraph rowValuePara = rowValue.AppendChild(new Paragraph()); Run rowValueRun = rowValuePara.AppendChild(new Run()); int lineCount = 0; foreach (string line in entry.Value) { rowValueRun.AppendChild(new Text(line)); if (++lineCount != entry.Value.Count) { rowValueRun.AppendChild(new Break()); } } tableRow.AppendChild(rowHeader); tableRow.AppendChild(rowValue); table.AppendChild(tableRow); } /* for (int i = 0; i <= data.GetUpperBound(0); i++) * { * var tr = new TableRow(); * for (int j = 0; j <= data.GetUpperBound(1); j++) * { * var tc = new TableCell(); * if (j == 0) * { * //Apply the same width at column 1 (0) * tc.Append(new TableCellWidth {Type = TableWidthUnitValues.Dxa, Width = "1821"}); * tc.Append(new TableCellProperties(new Shading{Val = ShadingPatternValues.Clear,Color = "auto",Fill = "##d3d4d6"})); * Paragraph para = tc.AppendChild(new Paragraph()); * Run run = para.AppendChild(new Run()); * RunProperties runProperties = getBold(run); * run.AppendChild(new Text(dataDict.)); * } * else * { * tc.AppendChild(new Paragraph(new Run(new Text(data[i, j])))); * } * * tr.AppendChild(tc); * } * if (data[i, 1] != "") * table.AppendChild(tr); * }*/ _body.AppendChild(table); _body.AppendChild(new Paragraph()); }
public Wordprocessing.Table CreateTable(int columnsCount) { Wordprocessing.Table table1 = new Wordprocessing.Table(); WorkbookPart workbookPart = _spreadsheetDocument.WorkbookPart; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); DocumentFormat.OpenXml.Spreadsheet.SheetData sheetData = worksheetPart.Worksheet.Elements<DocumentFormat.OpenXml.Spreadsheet.SheetData>().First(); //Задание свойств таблицы Wordprocessing.TableProperties tableProperties1 = new Wordprocessing.TableProperties(); Wordprocessing.TableStyle tableStyle1 = new Wordprocessing.TableStyle() { Val = "TableGrid" }; Wordprocessing.TableWidth tableWidth1 = new Wordprocessing.TableWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto }; Wordprocessing.TableBorders tableBorders1 = new Wordprocessing.TableBorders(); Wordprocessing.TopBorder topBorder1 = new Wordprocessing.TopBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.LeftBorder leftBorder1 = new Wordprocessing.LeftBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.BottomBorder bottomBorder1 = new Wordprocessing.BottomBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.RightBorder rightBorder1 = new Wordprocessing.RightBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.InsideHorizontalBorder insideHorizontalBorder1 = new Wordprocessing.InsideHorizontalBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.InsideVerticalBorder insideVerticalBorder1 = new Wordprocessing.InsideVerticalBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; tableBorders1.Append(topBorder1); tableBorders1.Append(leftBorder1); tableBorders1.Append(bottomBorder1); tableBorders1.Append(rightBorder1); tableBorders1.Append(insideHorizontalBorder1); tableBorders1.Append(insideVerticalBorder1); Wordprocessing.TableLook tableLook1 = new Wordprocessing.TableLook() { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); tableProperties1.Append(tableBorders1); tableProperties1.Append(tableLook1); table1.Append(tableProperties1); //Создание структуры таблицы Wordprocessing.TableGrid tableGrid1 = new Wordprocessing.TableGrid(); Wordprocessing.GridColumn gridColumn = new Wordprocessing.GridColumn() { Width = "9571" }; tableGrid1.Append(gridColumn); table1.Append(tableGrid1); //Добавление информации из Excel int j = 0; foreach (Spreadsheet.Row r in sheetData.Elements<Spreadsheet.Row>()) { j = 0; Wordprocessing.TableRow tableRow1 = new Wordprocessing.TableRow(); foreach (Spreadsheet.Cell c in r.Elements<Spreadsheet.Cell>()) { j++; string value = c.InnerText; if (c.DataType!= null && c.DataType.Value == CellValues.SharedString) { var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault(); if (stringTable != null) value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText; } TableRowExtension.AddCell(tableRow1,value); } for (int i=j; i < columnsCount; i++) TableRowExtension.AddCell(tableRow1, ""); table1.Append(tableRow1); } Wordprocessing.Table table2 = doptable(table1); Wordprocessing.Table table3 = doptable2(table2); return table3; }
/// <summary> /// The write table. /// </summary> /// <param name="t"> /// The t. /// </param> public void WriteTable(Table t) { this.body.Append(CreateParagraph(t.GetFullCaption(this.style), TableCaptionID)); var table = new DocumentFormat.OpenXml.Wordprocessing.Table(); var tableProperties1 = new TableProperties(); var tableStyle1 = new TableStyle { Val = "TableGrid" }; var tableWidth1 = new TableWidth { Width = "0", Type = TableWidthUnitValues.Auto }; var tableLook1 = new TableLook { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); tableProperties1.Append(tableLook1); var tableGrid1 = new TableGrid(); foreach (var tc in t.Columns) { // tc.Width var gridColumn1 = new GridColumn { Width = "3070" }; tableGrid1.Append(gridColumn1); } foreach (var row in t.Rows) { var tr = new TableRow(); if (row.IsHeader) { var trp = new TableRowProperties(); var tableHeader1 = new TableHeader(); trp.Append(tableHeader1); tr.Append(trp); } int j = 0; foreach (var c in row.Cells) { bool isHeader = row.IsHeader || t.Columns[j++].IsHeader; var cell = new TableCell(); var tcp = new TableCellProperties(); var borders = new TableCellBorders(); borders.Append( new BottomBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); borders.Append( new TopBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); borders.Append( new LeftBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); borders.Append( new RightBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); tcp.Append(borders); cell.Append(tcp); string styleID = isHeader ? "TableHeader" : "TableText"; cell.Append(CreateParagraph(c.Content, styleID)); tr.Append(cell); } table.Append(tr); } this.body.Append(table); }
/// <summary> /// Create new Word document with a two column table from a fixed array. /// </summary> /// <returns>Is valid document</returns> public bool CreateDocumentWithTableFromArray(string pFileName) { mHasException = false; var names = new[, ] { { "Karen", "Payne" }, { "Bill", "Smith" }, { "Jane", "Lebow" }, { "Jess", "Gallagher" } }; var fileName = Path.Combine(DocumentFolder, pFileName); if (File.Exists(fileName)) { File.Delete(fileName); } using (var document = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = document.AddMainDocumentPart(); mainPart.Document = new Document(); mainPart.Document.AppendChild(new Body()); var table = new Table(); // set borders TableProperties props = Helpers.CreateTableProperties(); table.AppendChild(props); // make table full width of page var tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct }; table.AppendChild(tableWidth); for (var outerIndex = 0; outerIndex <= names.GetUpperBound(0); outerIndex++) { var tableRow = new TableRow(); for (var innerIndex = 0; innerIndex <= names.GetUpperBound(1); innerIndex++) { var tableColumn = new TableCell(); tableColumn.Append(new Paragraph(new Run(new Text(names[outerIndex, innerIndex])))); // Assume you want columns that are automatically sized. tableColumn.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto })); tableRow.Append(tableColumn); } table.Append(tableRow); } mainPart.Document.Body.Append(table); mainPart.Document.Save(); } return(true); }
protected word.Table GetWordTable(DataTable dataTable) { word.Table table = new word.Table(); #region Set Table Properties word.TableProperties tableProperties = new word.TableProperties(); word.TableWidth tableWidth = new word.TableWidth() { Type = word.TableWidthUnitValues.Pct, Width = "5000" }; tableProperties.Append(tableWidth); UInt32Value borderWidth = UInt32Value.FromUInt32(5); word.TableBorders tableBorders = new word.TableBorders( new word.TopBorder { Val = new EnumValue <word.BorderValues>(word.BorderValues.Single), Size = borderWidth }, new word.BottomBorder { Val = new EnumValue <word.BorderValues>(word.BorderValues.Single), Size = borderWidth }, new word.LeftBorder { Val = new EnumValue <word.BorderValues>(word.BorderValues.Single), Size = borderWidth }, new word.RightBorder { Val = new EnumValue <word.BorderValues>(word.BorderValues.Single), Size = borderWidth }, new word.InsideHorizontalBorder { Val = new EnumValue <word.BorderValues>(word.BorderValues.Single), Size = borderWidth }, new word.InsideVerticalBorder { Val = new EnumValue <word.BorderValues>(word.BorderValues.Single), Size = borderWidth }); tableProperties.Append(tableBorders); table.AppendChild <word.TableProperties>(tableProperties); #endregion word.TableGrid tableGrid = new word.TableGrid(); table.Append(tableGrid); if (PrintTableHeader) { word.TableRow headerRow = new word.TableRow(); foreach (DataColumn dataColumn in dataTable.Columns) { word.GridColumn gridColumn = new word.GridColumn(); word.TableCell tableCell = new word.TableCell(); tableCell.Append(GetParagraph(GetColumnName(dataColumn))); headerRow.Append(tableCell); tableGrid.Append(gridColumn); } table.Append(headerRow); } foreach (DataRow row in dataTable.Rows) { word.TableRow tableRow = new word.TableRow(); foreach (object cellItem in row.ItemArray) { word.TableCell tableCell = new word.TableCell(); tableCell.Append(GetParagraph(cellItem.ToString())); tableRow.Append(tableCell); } table.Append(tableRow); } return(table); }