public override void Render()
        {
            // Receive parent data item context
            HierarchicalDataItem parentDataItem = GetDataContext();

            _data = _isDataReceivedDirectly ? _data : _templateProcessor.GetValue(_dataSourceTemplate, parentDataItem);

            bool isCanceled = CallBeforeRenderMethod();

            if (isCanceled)
            {
                ResultRange = ExcelHelper.CloneRange(Range);
                return;
            }

            ICustomEnumerator enumerator = null;

            try
            {
                enumerator = EnumeratorFactory.Create(_data) ?? new EnumerableEnumerator(new object[] { });
                IDictionary <IXLCell, IList <ParsedAggregationFunc> > totalCells = ParseTotalCells();
                DoAggregation(enumerator, totalCells.SelectMany(t => t.Value).ToArray(), parentDataItem);
                IXLWorksheet ws               = Range.Worksheet;
                dynamic      dataSource       = new ExpandoObject();
                var          dataSourceAsDict = (IDictionary <string, object>)dataSource;
                foreach (KeyValuePair <IXLCell, IList <ParsedAggregationFunc> > totalCell in totalCells)
                {
                    IList <ParsedAggregationFunc> aggFuncs = totalCell.Value;
                    foreach (ParsedAggregationFunc f in aggFuncs)
                    {
                        dataSourceAsDict[$"AggFunc_{f.UniqueName}"] = f.Result;
                    }
                }

                string rangeName = $"AggFuncs_{Guid.NewGuid():N}";
                Range.AddToNamed(rangeName, XLScope.Worksheet);

                var dataPanel = new ExcelDataSourcePanel(new[] { dataSource }, ws.NamedRange(rangeName), _report, _templateProcessor)
                {
                    Parent = Parent
                };
                dataPanel.Render();
                ResultRange = ExcelHelper.MergeRanges(Range, dataPanel.ResultRange);
            }
            finally
            {
                (enumerator as IDisposable)?.Dispose();
            }

            RemoveName();
            CallAfterRenderMethod();
        }
Exemple #2
0
        private IXLRange RenderTotalsTemplates(IList <ExcelDynamicColumn> columns)
        {
            string  template = _templateProcessor.WrapTemplate("Totals");
            IXLCell cell     = Range.CellsUsedWithoutFormulas().SingleOrDefault(c => Regex.IsMatch(c.Value.ToString(), $@"^{template}$", RegexOptions.IgnoreCase));

            if (cell == null)
            {
                return(null);
            }

            IXLWorksheet ws    = Range.Worksheet;
            IXLRange     range = ws.Range(cell, cell);

            bool isCanceled = CallBeforeRenderMethod(BeforeTotalsTemplatesRenderMethodName, range, columns);

            if (isCanceled)
            {
                return(range);
            }

            cell.Value = _templateProcessor.BuildDataItemTemplate(nameof(TotalsTemplatesHelper.Totals));
            string rangeName = $"Totals_{Guid.NewGuid():N}";

            range.AddToNamed(rangeName, XLScope.Worksheet);

            IList <string> totalsTemplates = new List <string>();

            foreach (ExcelDynamicColumn column in columns)
            {
                totalsTemplates.Add(column.AggregateFunction != AggregateFunction.NoAggregation
                    ? _templateProcessor.BuildAggregationFuncTemplate(column.AggregateFunction, column.Name)
                    : null);
            }

            var panel = new ExcelDataSourcePanel(totalsTemplates.Select(t => new TotalsTemplatesHelper {
                Totals = t
            }), ws.NamedRange(rangeName), _report, _templateProcessor)
            {
                ShiftType = ShiftType.Cells,
                Type      = Type == PanelType.Vertical ? PanelType.Horizontal : PanelType.Vertical,
            };

            panel.Render();

            SetColumnsWidth(panel.ResultRange, columns);
            SetCellsDisplayFormat(panel.ResultRange, columns);

            CallAfterRenderMethod(AfterTotalsTemplatesRenderMethodName, panel.ResultRange, columns);

            return(panel.ResultRange);
        }
Exemple #3
0
        private IXLRange RenderColumnNumbers(IList <ExcelDynamicColumn> columns)
        {
            string  template = _templateProcessor.WrapTemplate(@"Numbers(\((?<start>\d+)\))?");
            IXLCell cell     = Range.CellsUsedWithoutFormulas().SingleOrDefault(c => Regex.IsMatch(c.Value.ToString(), $@"^{template}$", RegexOptions.IgnoreCase));

            if (cell == null)
            {
                return(null);
            }

            IXLWorksheet ws    = Range.Worksheet;
            IXLRange     range = ws.Range(cell, cell);

            bool isCanceled = CallBeforeRenderMethod(BeforeNumbersRenderMethodName, range, columns);

            if (isCanceled)
            {
                return(range);
            }

            Match match = Regex.Match(cell.Value.ToString(), $@"^{template}$", RegexOptions.IgnoreCase);

            if (!int.TryParse(match.Groups["start"]?.Value, out int startNumber))
            {
                startNumber = 1;
            }

            cell.Value = _templateProcessor.BuildDataItemTemplate(nameof(ColumnNumbersHelper.Number));
            string rangeName = $"ColumnNumbers_{Guid.NewGuid():N}";

            range.AddToNamed(rangeName, XLScope.Worksheet);

            var panel = new ExcelDataSourcePanel(columns.Select((c, i) => new ColumnNumbersHelper {
                Number = i + startNumber
            }).ToList(),
                                                 ws.NamedRange(rangeName), _report, _templateProcessor)
            {
                ShiftType = ShiftType.Cells,
                Type      = Type == PanelType.Vertical ? PanelType.Horizontal : PanelType.Vertical,
            };

            panel.Render();

            SetColumnsWidth(panel.ResultRange, columns);
            CallAfterRenderMethod(AfterNumbersRenderMethodName, panel.ResultRange, columns);

            return(panel.ResultRange);
        }
Exemple #4
0
        public IXLRange RenderData(IXLRange dataRange)
        {
            string rangeName = $"DynamicPanelData_{Guid.NewGuid():N}";

            dataRange.AddToNamed(rangeName, XLScope.Worksheet);
            var dataPanel = new ExcelDataSourcePanel(_data, Range.Worksheet.NamedRange(rangeName), _report, _templateProcessor)
            {
                ShiftType = ShiftType,
                Type      = Type,
                GroupBy   = GroupBy,
                BeforeRenderMethodName         = BeforeDataRenderMethodName,
                AfterRenderMethodName          = AfterDataRenderMethodName,
                BeforeDataItemRenderMethodName = BeforeDataItemRenderMethodName,
                AfterDataItemRenderMethodName  = AfterDataItemRenderMethodName,
            };

            dataPanel.Render();
            return(dataPanel.ResultRange);
        }
Exemple #5
0
        private IXLRange RenderHeaders(IList <ExcelDynamicColumn> columns)
        {
            string  template = _templateProcessor.WrapTemplate("Headers");
            IXLCell cell     = Range.CellsUsedWithoutFormulas().SingleOrDefault(c => Regex.IsMatch(c.Value.ToString(), $@"^{template}$", RegexOptions.IgnoreCase));

            if (cell == null)
            {
                return(null);
            }

            IXLWorksheet ws    = Range.Worksheet;
            IXLRange     range = ws.Range(cell, cell);

            bool isCanceled = CallBeforeRenderMethod(BeforeHeadersRenderMethodName, range, columns);

            if (isCanceled)
            {
                return(range);
            }

            cell.Value = _templateProcessor.BuildDataItemTemplate(nameof(ExcelDynamicColumn.Caption));
            string rangeName = $"Headers_{Guid.NewGuid():N}";

            range.AddToNamed(rangeName, XLScope.Worksheet);

            var panel = new ExcelDataSourcePanel(columns, ws.NamedRange(rangeName), _report, _templateProcessor)
            {
                ShiftType = ShiftType.Cells,
                Type      = Type == PanelType.Vertical ? PanelType.Horizontal : PanelType.Vertical,
            };

            panel.Render();

            SetColumnsWidth(panel.ResultRange, columns);
            CallAfterRenderMethod(AfterHeadersRenderMethodName, panel.ResultRange, columns);

            return(panel.ResultRange);
        }