コード例 #1
0
        public void CloneUnknownElementTest()
        {
            string outerXml = "<a:txBody xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><a:p><a:r><a:t>Text in &lt;drawing&gt;.</a:t></a:r></a:p></a:txBody>";

            OpenXmlUnknownElement unknownElement = OpenXmlUnknownElement.CreateOpenXmlUnknownElement(outerXml);

            Assert.IsType <OpenXmlUnknownElement>(unknownElement.FirstChild);
            Assert.IsType <OpenXmlUnknownElement>(unknownElement.FirstChild.FirstChild);
            Assert.IsType <OpenXmlUnknownElement>(unknownElement.FirstChild.FirstChild.FirstChild);
            Assert.Equal("Text in <drawing>.", (unknownElement.FirstChild.FirstChild.FirstChild as OpenXmlUnknownElement).Text);

            Assert.Equal(outerXml, unknownElement.OuterXml);

            OpenXmlElement clonedElement = unknownElement.CloneNode(true);

            Assert.Equal("txBody", clonedElement.LocalName);
            Assert.Equal("a", clonedElement.Prefix);
            Assert.Equal("http://schemas.openxmlformats.org/drawingml/2006/main", clonedElement.NamespaceUri);
            Assert.IsType <OpenXmlUnknownElement>(clonedElement);

            Assert.IsType <OpenXmlUnknownElement>(clonedElement.FirstChild);
            Assert.IsType <OpenXmlUnknownElement>(clonedElement.FirstChild.FirstChild);
            Assert.IsType <OpenXmlUnknownElement>(clonedElement.FirstChild.FirstChild.FirstChild);
            Assert.Equal("Text in <drawing>.", (clonedElement.FirstChild.FirstChild.FirstChild as OpenXmlUnknownElement).Text);

            Assert.Equal(outerXml, clonedElement.OuterXml);

            clonedElement = unknownElement.CloneNode(false);

            Assert.Equal("txBody", clonedElement.LocalName);
            Assert.Equal("a", clonedElement.Prefix);
            Assert.Equal("http://schemas.openxmlformats.org/drawingml/2006/main", clonedElement.NamespaceUri);
            Assert.Null(clonedElement.FirstChild);
        }
コード例 #2
0
ファイル: TableBlock.cs プロジェクト: kevinfs/ReportGenerator
        private static void AddNewGridColumn(OpenXmlElement tableGrid, OpenXmlElement headerRow, OpenXmlElement contentRow)
        {
            var columns = tableGrid.Descendants <OXD.GridColumn>().ToList();

            if (columns.Count == 0 || !columns.Any())
            {
                return;
            }
            var    headerLastCell  = headerRow.Descendants <OXD.TableCell>().Last();
            var    contentLastCell = contentRow.Descendants <OXD.TableCell>().Last();
            double tableWidth      = columns.Sum(_ => Convert.ToInt32(_.Width.Value));
            var    newColWidth     = Math.Floor(tableWidth / columns.Count);

            foreach (var col in columns)
            {
                col.Width = col.Width > 0 ? Convert.ToInt64(Math.Floor((tableWidth - newColWidth) / (tableWidth / col.Width))) : 0;
            }

            OXD.GridColumn newcol = new OXD.GridColumn()
            {
                Width = Convert.ToInt64(newColWidth)
            };
            OXD.ExtensionList init_extlst = newcol.Descendants <OXD.ExtensionList>().FirstOrDefault();
            if (init_extlst != null)
            {
                OXD.Extension         init_ext   = init_extlst.Descendants <OXD.Extension>().FirstOrDefault();
                OpenXmlUnknownElement init_colId = init_ext?.GetFirstChild <OpenXmlUnknownElement>();
                OpenXmlUnknownElement new_colId  = init_colId?.CloneNode(true) as OpenXmlUnknownElement;
                if (new_colId != null)
                {
                    OpenXmlAttribute val = new_colId.GetAttributes().FirstOrDefault();
                    val.Value = new Random().Next().ToString();
                    new_colId.SetAttribute(val);
                    init_ext.ReplaceChild(new_colId, init_colId);
                }
            }
            tableGrid.InsertAfter(newcol, columns.Last());
            headerRow.InsertAfter((OXD.TableCell)headerLastCell.CloneNode(true), headerLastCell);
            contentRow.InsertAfter((OXD.TableCell)contentLastCell.CloneNode(true), contentLastCell);
        }
コード例 #3
0
ファイル: TableBlock.cs プロジェクト: kevinfs/ReportGenerator
        private static void UpdatePowerPointBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options)
        {
            if (null != content && block is OXP.GraphicFrame)
            {
                var       randomValue = new Random();
                OXD.Table initTable   = block.Descendants <OXD.Table>().FirstOrDefault();
                if (null == initTable)
                {
                    return;
                }
                try
                {
                    OXD.Table    table              = initTable.CloneNode(true) as OXD.Table;
                    OXD.TableRow headerRowTemplate  = table?.Descendants <OXD.TableRow>().First().CloneNode(true) as OXD.TableRow;
                    OXD.TableRow contentRowTemplate = table?.Descendants <OXD.TableRow>().Skip(1).First().CloneNode(true) as OXD.TableRow;

                    ModifyPowerPointRowTextContent(headerRowTemplate, string.Empty);
                    ModifyPowerPointRowTextContent(contentRowTemplate, string.Empty);

                    #region Column Number Management
                    List <OXD.GridColumn> columns = table?.TableGrid.Descendants <OXD.GridColumn>().ToList();
                    if (columns != null && columns.Count < content.NbColumns)
                    {
                        int nbNewColumn = content.NbColumns - columns.Count;
                        for (int i = 0, lim = nbNewColumn; i < lim; i++)
                        {
                            AddNewGridColumn(table.TableGrid, headerRowTemplate, contentRowTemplate);
                        }
                    }
                    else if (columns != null && columns.Count > content.NbColumns)
                    {
                        for (int i = content.NbColumns, lim = columns.Count; i < lim; i++)
                        {
                            RemoveLastGridColumn(table.TableGrid);
                        }
                    }
                    #endregion Column Number Management

                    int idx   = 0;
                    int nbrow = 0;
                    List <OXD.TableCell> headerCells  = headerRowTemplate?.Descendants <OXD.TableCell>().Select(_ => _.CloneNode(true) as OXD.TableCell).ToList();
                    List <OXD.TableCell> contentCells = contentRowTemplate?.Descendants <OXD.TableCell>().Select(_ => _.CloneNode(true) as OXD.TableCell).ToList();
                    headerRowTemplate?.RemoveAllChildren <OXD.TableCell>();
                    OXD.TableRow row = headerRowTemplate;

                    table?.RemoveAllChildren <OXD.TableRow>();
                    for (int i = 0; i < content.Data.Count(); i++)
                    {
                        string        item = content.Data.ToArray()[i];
                        OXD.TableCell cell;
                        if (content.HasColumnHeaders && 0 == nbrow)
                        {
                            cell = headerCells?[idx].CloneNode(true) as OXD.TableCell;
                        }
                        else
                        {
                            cell = contentCells?[idx].CloneNode(true) as OXD.TableCell;
                        }
                        ModifyPowerPointCellTextContent(cell, item);

                        if (content.HasCellsAttributes())
                        {
                            CellAttributes attributes = content.CellsAttributes.FirstOrDefault(a => a.Index == i);
                            if (attributes?.BackgroundColor != null)
                            {
                                Color myColor = attributes.BackgroundColor;
                                OXD.RgbColorModelHex backColor = new OXD.RgbColorModelHex()
                                {
                                    Val = $"{myColor.R:X2}{myColor.G:X2}{myColor.B:X2}"
                                };
                                OXD.SolidFill solidFill = new OXD.SolidFill();
                                solidFill.Append(backColor);
                                OXD.TableCellProperties props     = cell?.Descendants <OXD.TableCellProperties>().FirstOrDefault();
                                OXD.TableCellProperties new_props = (props != null) ? props.CloneNode(true) as OXD.TableCellProperties : new OXD.TableCellProperties();

                                OXD.SolidFill oldFill = new_props?.Descendants <OXD.SolidFill>().FirstOrDefault();
                                oldFill?.Remove();
                                new_props?.InsertAfter(solidFill, new_props.LastChild);
                                if (props != null)
                                {
                                    cell.ReplaceChild(new_props, props);
                                }
                                else
                                {
                                    cell?.AppendChild(new_props);
                                }
                            }
                        }

                        //row.Append(cell); => in office 2016, element <extLst> should absolutely be in the latest position in a row
                        row?.InsertBefore(cell, row.Descendants <OXD.ExtensionList>().FirstOrDefault());

                        OXD.ExtensionList init_extlst = row?.Descendants <OXD.ExtensionList>().FirstOrDefault();
                        if (init_extlst != null)
                        {
                            OXD.Extension         init_ext   = init_extlst.Descendants <OXD.Extension>().FirstOrDefault();
                            OpenXmlUnknownElement init_rowId = init_ext?.GetFirstChild <OpenXmlUnknownElement>();
                            OpenXmlUnknownElement new_rowId  = init_rowId?.CloneNode(true) as OpenXmlUnknownElement;
                            if (new_rowId != null)
                            {
                                OpenXmlAttribute val = new_rowId.GetAttributes().FirstOrDefault();
                                val.Value = randomValue.Next().ToString();
                                new_rowId.SetAttribute(val);
                                init_ext.ReplaceChild(new_rowId, init_rowId);
                            }
                        }

                        idx = ++idx % content.NbColumns;
                        if (0 != idx)
                        {
                            continue;
                        }
                        // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                        if (null != row)
                        {
                            table.Append(row);
                            nbrow++;
                        }
                        row = contentRowTemplate?.CloneNode(true) as OXD.TableRow;
                        row?.RemoveAllChildren <OXD.TableCell>();
                    }
                    initTable.Parent.ReplaceChild(table, initTable);
                }
                catch (Exception exception)
                {
                    LogHelper.Instance.LogErrorFormat("An unhandled exception was thrown during table block content generation : '{0}'", exception.ToString());
                    if (initTable.Descendants <OXD.TableRow>() != null && !initTable.Descendants <OXD.TableRow>().Any())
                    {
                        foreach (var row in initTable.Descendants <OXD.TableRow>().Skip(1))
                        {
                            ModifyPowerPointRowTextContent(row, string.Empty);
                        }
                    }
                }
            }
            else
            {
                LogHelper.Instance.LogErrorFormat("Impossible to load data in table block with a block source of type \"{0}\"", block?.GetType().ToString() ?? "null");
            }
        }