예제 #1
0
        private static void UpdatePowerPointBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options)
        {
            if (null != content && block is OXP.GraphicFrame)
            {
                OXD.Table initTable = (OXD.Table)block.Descendants <OXD.Table>().FirstOrDefault();
                if (null != initTable)
                {
                    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.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.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>();
                        foreach (var item in content.Data)
                        {
                            OXD.TableCell cell = null;
                            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);

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

                            idx = ++idx % content.NbColumns;
                            if (0 == idx)
                            {
                                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 (null != initTable)
                        {
                            if (null != initTable.Descendants <OXD.TableRow>() && 1 > initTable.Descendants <OXD.TableRow>().Count())
                            {
                                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}\"", null != block ? block.GetType().ToString() : "null");
            }
        }
예제 #2
0
        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");
            }
        }