/// <summary> /// Get table gridCols information and convert to twip to Points. /// </summary> /// <param name="table"></param> /// <returns></returns> private float[] getTableGridCols(Word.Table table) { float[] grids = null; if (table == null) { return(grids); } // Get grids and their width Word.TableGrid grid = (Word.TableGrid)table.Elements <Word.TableGrid>().FirstOrDefault(); if (grid != null) { List <Word.GridColumn> gridCols = grid.Elements <Word.GridColumn>().ToList(); if (gridCols.Count > 0) { grids = new float[gridCols.Count]; for (int i = 0; i < gridCols.Count; i++) { if (gridCols[i].Width != null) { grids[i] = Tools.ConvertToPoint(gridCols[i].Width.Value, Tools.SizeEnum.TwentiethsOfPoint, -1f); } else { grids[i] = 0f; } } } } return(grids); }
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); }
private static void FillHeaderTableGrid(OpenXmlElement table) { var tableGrid = new TableGrid(); tableGrid.AppendChild(new GridColumn { Width = StringValue.FromString("2000") }); tableGrid.AppendChild(new GridColumn { Width = StringValue.FromString("2550") }); tableGrid.AppendChild(new GridColumn { Width = StringValue.FromString("3825") }); table.AppendChild(tableGrid); }
/// <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> /// 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); }
private static void UpdateWordBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options) { if (null != content && block is OXW.Table) { OXW.Table table = ((OXW.Table)block).CloneNode(true) as OXW.Table; OXW.TableRow headerRowTemplate = table.Descendants <OXW.TableRow>().First().CloneNode(true) as OXW.TableRow; OXW.TableRow contentRowTemplate = table.Descendants <OXW.TableRow>().Skip(1).First().CloneNode(true) as OXW.TableRow; #region Column number management OXW.TableGrid tablegrid = table.Descendants <OXW.TableGrid>().FirstOrDefault(); if (null != tablegrid) { List <OXW.GridColumn> columns = tablegrid.Descendants <OXW.GridColumn>().ToList(); if (null != columns && content.NbColumns != columns.Count) { if (content.NbColumns < columns.Count) { for (int i = columns.Count - 1, lim = content.NbColumns - 1; i > lim; i--) { tablegrid.RemoveChild <OXW.GridColumn>(columns[i]); } } else { for (int i = 0, lim = content.NbColumns - columns.Count; i < lim; i++) { tablegrid.AppendChild <OXW.GridColumn>(new OXW.GridColumn() { Width = "1000" }); } } } } #endregion Column number management ModifyWordRowTextContent(headerRowTemplate, string.Empty); ModifyWordRowTextContent(contentRowTemplate, string.Empty); int idx = 0; int nbrow = 0; List <OXW.TableCell> headerCells = headerRowTemplate.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); List <OXW.TableCell> contentCells = contentRowTemplate.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); headerRowTemplate.RemoveAllChildren <OXW.TableCell>(); OXW.TableRow row = headerRowTemplate; int headerCellsCount = headerCells.Count; int contentCellsCount = headerCells.Count; table.RemoveAllChildren <OXW.TableRow>(); foreach (var item in content.Data) { if (null != item) { OXW.TableCell cell = null; if (content.HasColumnHeaders && 0 == nbrow) { cell = headerCells[idx % headerCellsCount].CloneNode(true) as OXW.TableCell; } else { cell = contentCells[idx % contentCellsCount].CloneNode(true) as OXW.TableCell; } ModifyWordCellTextContent(cell, item); row.Append(cell); } idx = ++idx % content.NbColumns; if (0 == idx) { if (null != row) { table.Append(row); nbrow++; } row = contentRowTemplate.CloneNode(true) as OXW.TableRow; row.RemoveAllChildren <OXW.TableCell>(); } } var blockSdt = block.Ancestors <OXW.SdtBlock>().First(); blockSdt.Parent.ReplaceChild(table, blockSdt); } else { LogHelper.Instance.LogErrorFormat("Impossible to load data in Table block with a block source of type \"{0}\"", null != block ? block.GetType().ToString() : "null"); } }
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); }
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; }
private static void UpdateWordBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options) { if (null != content && block is OXW.Table) { OXW.Table table = ((OXW.Table)block).CloneNode(true) as OXW.Table; OXW.TableRow headerRowTemplate = table?.Descendants <OXW.TableRow>().First().CloneNode(true) as OXW.TableRow; OXW.TableRow contentRowTemplate = table?.Descendants <OXW.TableRow>().Skip(1).First().CloneNode(true) as OXW.TableRow; #region Column number management OXW.TableGrid tablegrid = table?.Descendants <OXW.TableGrid>().FirstOrDefault(); List <OXW.GridColumn> columns = tablegrid?.Descendants <OXW.GridColumn>().ToList(); if (columns != null && content.NbColumns != columns.Count) { if (content.NbColumns < columns.Count) { for (int i = columns.Count - 1, lim = content.NbColumns - 1; i > lim; i--) { tablegrid.RemoveChild(columns[i]); } } else { for (int i = 0, lim = content.NbColumns - columns.Count; i < lim; i++) { tablegrid.AppendChild(new OXW.GridColumn() { Width = "200" }); } } } #endregion Column number management ModifyWordRowTextContent(headerRowTemplate, string.Empty, string.Empty, string.Empty); ModifyWordRowTextContent(contentRowTemplate, string.Empty, string.Empty, string.Empty); int idx = 0; int nbrow = 0; List <OXW.TableCell> headerCells = headerRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); List <OXW.TableCell> contentCells = contentRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); headerRowTemplate?.RemoveAllChildren <OXW.TableCell>(); OXW.TableRow row = headerRowTemplate; if (headerCells != null) { int headerCellsCount = headerCells.Count; int contentCellsCount = headerCells.Count; table.RemoveAllChildren <OXW.TableRow>(); for (int i = 0; i < content.Data.Count(); i++) { var item = content.Data.ToArray()[i]; if (null != item) { OXW.TableCell cell; if (content.HasColumnHeaders && 0 == nbrow) { cell = headerCells[idx % headerCellsCount].CloneNode(true) as OXW.TableCell; } else { cell = contentCells?[idx % contentCellsCount].CloneNode(true) as OXW.TableCell; } string txtColor = string.Empty; string effect = string.Empty; if (content.HasCellsAttributes()) { CellAttributes attributes = content.CellsAttributes.FirstOrDefault(a => a.Index == i); if (attributes != null) { OXW.TableCellProperties tcp = new OXW.TableCellProperties( new OXW.TableCellWidth { Type = OXW.TableWidthUnitValues.Auto, } ); // Create the Shading object OXW.Shading shading = new OXW.Shading() { Color = "auto", Fill = ColorTranslator.ToHtml(attributes.BackgroundColor), Val = OXW.ShadingPatternValues.Clear }; // Add the Shading object to the TableCellProperties object tcp.Append(shading); // Add the TableCellProperties object to the TableCell object cell?.Append(tcp); txtColor = $"{attributes.FontColor.R:X2}{attributes.FontColor.G:X2}{attributes.FontColor.B:X2}"; effect = attributes.Effect; } } ModifyWordCellTextContent(cell, item, txtColor, effect); row?.Append(cell); } idx = ++idx % content.NbColumns; if (0 != idx) { continue; } if (null != row) { table.Append(row); nbrow++; } row = contentRowTemplate?.CloneNode(true) as OXW.TableRow; row?.RemoveAllChildren <OXW.TableCell>(); } } var blockSdtAncestor = block.Ancestors <OXW.SdtBlock>(); if (0 != blockSdtAncestor.ToList().Count) { // case table is in a content control var blockStd = block.Ancestors <OXW.SdtBlock>().First(); blockStd.Parent.ReplaceChild(table, blockStd); } else { // case table is directly in the document var blockStd = block; blockStd.Parent.ReplaceChild(table, blockStd); } } else { LogHelper.Instance.LogErrorFormat("Impossible to load data in Table block with a block source of type \"{0}\"", block?.GetType().ToString() ?? "null"); } }