示例#1
0
        public void TestGetNearestNamedPanelTest()
        {
            XLWorkbook   wb                = new XLWorkbook();
            IXLWorksheet ws                = wb.AddWorksheet("Test");
            var          excelReport       = Substitute.For <object>();
            var          templateProcessor = Substitute.For <ITemplateProcessor>();

            IXLRange range = ws.Range(1, 1, 3, 4);

            range.AddToNamed("Parent", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("Parent");

            IXLRange childRange = ws.Range(2, 1, 3, 4);

            IXLRange childOfChildRange = ws.Range(3, 1, 3, 4);

            childOfChildRange.AddToNamed("ChildOfChild", XLScope.Worksheet);
            IXLNamedRange childOfChildNamedRange = ws.NamedRange("ChildOfChild");

            IExcelPanel childOfChildPanel = new ExcelNamedPanel(childOfChildNamedRange, excelReport, templateProcessor);
            IExcelPanel childPanel        = new ExcelPanel(childRange, excelReport, templateProcessor);
            IExcelPanel parentPanel       = new ExcelNamedPanel(namedRange, excelReport, templateProcessor);

            MethodInfo method = typeof(ExcelNamedPanel).GetMethod("GetNearestNamedParent", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.IsNull(method.Invoke(childOfChildPanel, null));

            childOfChildPanel.Parent = childPanel;
            Assert.IsNull(method.Invoke(childOfChildPanel, null));

            childPanel.Parent = parentPanel;
            Assert.AreSame(parentPanel, method.Invoke(childOfChildPanel, null));
        }
示例#2
0
        public void TestRemoveAllNamesRecursive()
        {
            XLWorkbook   wb                = new XLWorkbook();
            IXLWorksheet ws                = wb.AddWorksheet("Test");
            var          excelReport       = Substitute.For <object>();
            var          templateProcessor = Substitute.For <ITemplateProcessor>();

            IXLRange range = ws.Range(1, 1, 3, 4);

            range.AddToNamed("Parent", XLScope.Worksheet);
            IXLNamedRange    namedRange  = ws.NamedRange("Parent");
            IExcelNamedPanel parentPanel = new ExcelNamedPanel(namedRange, excelReport, templateProcessor);

            IXLRange childRange1 = ws.Range(1, 1, 1, 4);

            childRange1.AddToNamed("Child", XLScope.Worksheet);
            IXLNamedRange    namedChildRange = ws.NamedRange("Child");
            IExcelNamedPanel childPanel1     = new ExcelNamedPanel(namedChildRange, excelReport, templateProcessor);

            childPanel1.Parent = parentPanel;

            IXLRange    childRange2 = ws.Range(2, 1, 3, 4);
            IExcelPanel childPanel2 = new ExcelPanel(childRange2, excelReport, templateProcessor);

            childPanel2.Parent = parentPanel;

            parentPanel.Children = new List <IExcelPanel> {
                childPanel1, childPanel2
            };

            IXLRange childOfChild1Range = ws.Range(1, 1, 1, 4);

            childOfChild1Range.AddToNamed("ChildOfChild1", XLScope.Worksheet);
            IXLNamedRange    namedChildOfChild1RangeRange = ws.NamedRange("ChildOfChild1");
            IExcelNamedPanel childOfChild1Panel           = new ExcelNamedPanel(namedChildOfChild1RangeRange, excelReport, templateProcessor);

            childOfChild1Panel.Parent = childPanel1;
            childPanel1.Children      = new List <IExcelPanel> {
                childOfChild1Panel
            };

            IXLRange childOfChild2Range = ws.Range(3, 1, 3, 4);

            childOfChild2Range.AddToNamed("ChildOfChild2", XLScope.Worksheet);
            IXLNamedRange    namedChildOfChild2RangeRange = ws.NamedRange("ChildOfChild2");
            IExcelNamedPanel childOfChild2Panel           = new ExcelNamedPanel(namedChildOfChild2RangeRange, excelReport, templateProcessor);

            childOfChild2Panel.Parent = childPanel2;
            childPanel2.Children      = new List <IExcelPanel> {
                childOfChild2Panel
            };

            ExcelNamedPanel.RemoveAllNamesRecursive(parentPanel);
            Assert.AreEqual(0, ws.NamedRanges.Count());

            //wb.SaveAs("test.xlsx");
        }
        public void TestPanelWithNoData()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");

            IXLRange range = ws.Range(1, 1, 1, 5);

            range.AddToNamed("Test", XLScope.Worksheet);

            ws.Cell(1, 1).Value = "Plain text";
            ws.Cell(1, 2).Value = "{Sum(di:Sum)}";
            ws.Cell(1, 3).Value = "{ Custom(DI:Sum, CustomAggregation, PostAggregation)  }";
            ws.Cell(1, 4).Value = "{Min(di:Sum)}";
            ws.Cell(1, 5).Value = "Text1 {count(di:Name)} Text2 {avg(di:Sum, , PostAggregationRound)} Text3 {Max(di:Sum)}";

            var panel = new ExcelTotalsPanel("m:DataProvider:GetEmptyIEnumerable()", ws.NamedRange("Test"), report, report.TemplateProcessor);

            panel.Render();

            Assert.AreEqual(range, panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(TotalsPanelRenderTest),
                                                                                 nameof(TestPanelWithNoData)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
示例#4
0
        public void TestParseTotalCellsErrors()
        {
            XLWorkbook   wb = new XLWorkbook();
            IXLWorksheet ws = wb.AddWorksheet("Test");

            IXLRange range = ws.Range(1, 1, 1, 1);

            range.AddToNamed("Test", XLScope.Worksheet);

            var templateProcessor = Substitute.For <ITemplateProcessor>();

            templateProcessor.LeftTemplateBorder.Returns("<");
            templateProcessor.RightTemplateBorder.Returns(">");
            templateProcessor.MemberLabelSeparator.Returns("-");
            templateProcessor.DataItemMemberLabel.Returns("d");

            var report = new TestReport
            {
                TemplateProcessor = templateProcessor,
                Workbook          = wb
            };

            var        panel  = new ExcelTotalsPanel("Stub", ws.NamedRange("Test"), report, report.TemplateProcessor);
            MethodInfo method = panel.GetType().GetMethod("ParseTotalCells", BindingFlags.Instance | BindingFlags.NonPublic);

            ws.Cell(1, 1).Value = "<Sum(d-Val, fn1, fn2, fn3)>";
            ExceptionAssert.ThrowsBaseException <InvalidOperationException>(() => method.Invoke(panel, null), "Aggregation function must have at least one but no more than 3 parameters");
        }
        public void TestPanelRenderEvents()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 2, 3);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:Name}";
            ws.Cell(2, 3).Value = "{di:Date}";

            var panel = new ExcelDataSourcePanel("m:DataProvider:GetIEnumerable()", ws.NamedRange("TestRange"), report, report.TemplateProcessor)
            {
                BeforeRenderMethodName         = "TestExcelDataSourcePanelBeforeRender",
                AfterRenderMethodName          = "TestExcelDataSourcePanelAfterRender",
                BeforeDataItemRenderMethodName = "TestExcelDataItemPanelBeforeRender",
                AfterDataItemRenderMethodName  = "TestExcelDataItemPanelAfterRender",
            };

            panel.Render();

            Assert.AreEqual(ws.Range(2, 2, 4, 2), panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelIEnumerableRenderTest),
                                                                                 nameof(TestPanelRenderEvents)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void TestRenderEmptyDataSet()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 2, 6);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:Id}";
            ws.Cell(2, 3).Value = "{di:Name}";
            ws.Cell(2, 4).Value = "{di:IsVip}";
            ws.Cell(2, 5).Value = "{di:Description}";
            ws.Cell(2, 6).Value = "{di:Type}";

            var panel = new ExcelDataSourcePanel("m:DataProvider:GetEmptyDataSet()", ws.NamedRange("TestRange"), report, report.TemplateProcessor);

            panel.Render();

            Assert.IsNull(panel.ResultRange);

            Assert.AreEqual(0, ws.CellsUsed(XLCellsUsedOptions.Contents).Count());

            Assert.AreEqual(0, ws.NamedRanges.Count());
            Assert.AreEqual(0, ws.Workbook.NamedRanges.Count());

            Assert.AreEqual(1, ws.Workbook.Worksheets.Count);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void TestRenderIEnumerableVerticalNoShift()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 3, 5);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            range.FirstCell().Style.Border.SetTopBorder(XLBorderStyleValues.Thin);
            range.FirstCell().Style.Border.SetBottomBorder(XLBorderStyleValues.Thin);
            range.FirstCell().Style.Border.SetLeftBorder(XLBorderStyleValues.Thin);

            ws.Cell(3, 5).Style.Border.SetBottomBorder(XLBorderStyleValues.Thin);

            ws.Cell(2, 4).DataType = XLDataType.Number;
            //ws.Cell(2, 4).DataType = XLCellValues.Number;

            ws.Cell(2, 2).Value = "{di:Name}";
            ws.Cell(2, 3).Value = "{di:Date}";
            ws.Cell(2, 4).Value = "{di:Sum}";
            ws.Cell(2, 5).Value = "{di:Contacts}";
            ws.Cell(3, 2).Value = "{di:Contacts.Phone}";
            ws.Cell(3, 3).Value = "{di:Contacts.Fax}";
            ws.Cell(3, 4).Value = "{p:StrParam}";

            ws.Cell(1, 1).Value = "{di:Name}";
            ws.Cell(4, 1).Value = "{di:Name}";
            ws.Cell(1, 6).Value = "{di:Name}";
            ws.Cell(4, 6).Value = "{di:Name}";
            ws.Cell(3, 1).Value = "{di:Name}";
            ws.Cell(3, 6).Value = "{di:Name}";
            ws.Cell(1, 4).Value = "{di:Name}";
            ws.Cell(4, 4).Value = "{di:Name}";
            ws.Cell(8, 5).Value = "{di:Date}";

            ws.Cell(8, 5).Style.Border.SetTopBorder(XLBorderStyleValues.Thin);
            ws.Cell(8, 5).Style.Border.SetRightBorder(XLBorderStyleValues.Thin);
            ws.Cell(8, 5).Style.Border.SetBottomBorder(XLBorderStyleValues.Thin);
            ws.Cell(8, 5).Style.Border.SetLeftBorder(XLBorderStyleValues.Thin);

            ws.Cell(8, 5).Style.Border.SetTopBorderColor(XLColor.Red);
            ws.Cell(8, 5).Style.Border.SetRightBorderColor(XLColor.Red);
            ws.Cell(8, 5).Style.Border.SetBottomBorderColor(XLColor.Red);
            ws.Cell(8, 5).Style.Border.SetLeftBorderColor(XLColor.Red);

            var panel = new ExcelDataSourcePanel("m:DataProvider:GetIEnumerable()", ws.NamedRange("TestRange"), report, report.TemplateProcessor)
            {
                ShiftType = ShiftType.NoShift,
            };

            panel.Render();

            Assert.AreEqual(ws.Range(2, 2, 7, 5), panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelIEnumerableRenderTest),
                                                                                 nameof(TestRenderIEnumerableVerticalNoShift)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void TestCreateSimplePanel()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("s_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("s_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings
            {
                SimplePanelPrefix    = "s",
                PanelPrefixSeparator = "_",
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            var panel   = (ExcelPanel)factory.Create(namedRange, new Dictionary <string, string>
            {
                [nameof(ExcelPanel.Type)]                   = PanelType.Horizontal.ToString(),
                [nameof(ExcelPanel.ShiftType)]              = ShiftType.Row.ToString(),
                [nameof(ExcelPanel.RenderPriority)]         = "5",
                [nameof(ExcelPanel.BeforeRenderMethodName)] = "BeforeRenderMethodName",
                [nameof(ExcelPanel.AfterRenderMethodName)]  = "AfterRenderMethodName",
            });

            Assert.AreEqual(PanelType.Horizontal, panel.Type);
            Assert.AreEqual(ShiftType.Row, panel.ShiftType);
            Assert.AreEqual(5, panel.RenderPriority);
            Assert.AreEqual("BeforeRenderMethodName", panel.BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethodName", panel.AfterRenderMethodName);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));

            namedRange.Delete();
            range.AddToNamed("SS--Test", XLScope.Workbook);
            namedRange = wb.NamedRange("SS--Test");

            parseSettings.SimplePanelPrefix    = "ss";
            parseSettings.PanelPrefixSeparator = "--";
            factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            panel   = (ExcelPanel)factory.Create(namedRange, null);

            Assert.IsInstanceOf <ExcelPanel>(panel);
            Assert.AreEqual(PanelType.Vertical, panel.Type);
            Assert.AreEqual(ShiftType.Cells, panel.ShiftType);
            Assert.AreEqual(0, panel.RenderPriority);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.BeforeRenderMethodName);
            Assert.IsNull(panel.AfterRenderMethodName);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
        }
        public void TestRenderEmptyDictionary()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range1 = ws.Range(2, 2, 4, 2);

            range1.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{Headers}";
            ws.Cell(3, 2).Value = "{Data}";
            ws.Cell(4, 2).Value = "{Totals}";

            IXLRange range2 = ws.Range(7, 2, 9, 2);

            range2.AddToNamed("TestRange2", XLScope.Worksheet);

            ws.Cell(7, 2).Value = "{Headers}";
            ws.Cell(8, 2).Value = "{Data}";
            ws.Cell(9, 2).Value = "{Totals}";

            IDictionary <string, object> data1 = new Dictionary <string, object>();
            var panel1 = new ExcelDataSourceDynamicPanel(data1, ws.NamedRange("TestRange"), report, report.TemplateProcessor);

            panel1.Render();

            Assert.AreEqual(ws.Range(2, 2, 3, 3), panel1.ResultRange);

            IEnumerable <KeyValuePair <string, object> > data2 = new List <KeyValuePair <string, object> >();
            var panel2 = new ExcelDataSourceDynamicPanel(data2, ws.NamedRange("TestRange2"), report, report.TemplateProcessor);

            panel2.Render();

            Assert.AreEqual(ws.Range(6, 2, 7, 3), panel2.ResultRange);

            Assert.AreEqual(4, ws.CellsUsed(XLCellsUsedOptions.Contents).Count());
            Assert.AreEqual("Key", ws.Cell(2, 2).Value);
            Assert.AreEqual("Value", ws.Cell(2, 3).Value);
            Assert.AreEqual("Key", ws.Cell(6, 2).Value);
            Assert.AreEqual("Value", ws.Cell(6, 3).Value);

            //report.Workbook.SaveAs("test.xlsx");
        }
示例#10
0
        public void TestRenderDictionary()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range1 = ws.Range(2, 2, 2, 3);

            range1.AddToNamed("TestRange", XLScope.Worksheet);

            IXLRange range2 = ws.Range(2, 5, 2, 6);

            range2.AddToNamed("TestRange2", XLScope.Worksheet);

            ws.Cell(1, 2).Value = "Key";
            ws.Cell(1, 3).Value = "Value";
            ws.Cell(2, 2).Value = "{di:Key}";
            ws.Cell(2, 3).Value = "{di:Value}";

            ws.Cell(1, 5).Value = "Key";
            ws.Cell(1, 6).Value = "Value";
            ws.Cell(2, 5).Value = "{di:Key}";
            ws.Cell(2, 6).Value = "{di:Value}";

            IDictionary <string, object> data1 = new DataProvider().GetDictionaryEnumerable().First();
            var panel1 = new ExcelDataSourcePanel(data1, ws.NamedRange("TestRange"), report, report.TemplateProcessor);

            panel1.Render();

            IEnumerable <KeyValuePair <string, object> > data2 = new DataProvider().GetDictionaryEnumerable().First()
                                                                 .Select(x => new KeyValuePair <string, object>(x.Key, x.Value));
            var panel2 = new ExcelDataSourcePanel(data2, ws.NamedRange("TestRange2"), report, report.TemplateProcessor);

            panel2.Render();

            Assert.AreEqual(ws.Range(2, 2, 4, 3), panel1.ResultRange);
            Assert.AreEqual(ws.Range(2, 5, 4, 6), panel2.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelDictionaryRenderTest),
                                                                                 nameof(TestRenderDictionary)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void Test_VerticalPageBreaks_WithSimplePanel_RowShift()
        {
            var          report      = new TestReport();
            IXLWorksheet ws          = report.Workbook.AddWorksheet("Test");
            IXLRange     parentRange = ws.Range(2, 2, 5, 4);

            parentRange.AddToNamed("ParentRange", XLScope.Worksheet);

            parentRange.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;

            IXLRange child1 = ws.Range(2, 3, 5, 3);

            child1.AddToNamed("ChildRange1", XLScope.Worksheet);

            IXLRange child2 = ws.Range(2, 4, 5, 4);

            child2.AddToNamed("ChildRange2", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:Name}";
            ws.Cell(3, 2).Value = "{di:Date}";

            ws.Cell(3, 3).Value = "{di:Field1}";
            ws.Cell(4, 3).Value = "{di:Field2}";
            ws.Cell(5, 3).Value = "{di:parent:Sum}";

            ws.Cell(2, 4).Value = "{VertPageBreak}";

            var parentPanel = new ExcelDataSourcePanel("m:DataProvider:GetIEnumerable()", ws.NamedRange("ParentRange"), report, report.TemplateProcessor)
            {
                Type      = PanelType.Horizontal,
                ShiftType = ShiftType.Row,
            };
            var childPanel1 = new ExcelDataSourcePanel("m:DataProvider:GetChildIEnumerable(di:Name)", ws.NamedRange("ChildRange1"), report, report.TemplateProcessor)
            {
                Parent    = parentPanel,
                Type      = PanelType.Horizontal,
                ShiftType = ShiftType.Row,
            };
            var childPanel2 = new ExcelNamedPanel(ws.NamedRange("ChildRange2"), report, report.TemplateProcessor)
            {
                Parent = parentPanel,
            };

            parentPanel.Children = new[] { childPanel1, childPanel2 };
            parentPanel.Render();

            Assert.AreEqual(ws.Range(2, 2, 5, 12), parentPanel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelRender_WithGrouping_PageBreaks_Test),
                                                                                 "Test_VerticalPageBreaks"), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        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();
        }
示例#13
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);
        }
        public void TestRenderDictionaryEnumerable()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range1 = ws.Range(2, 2, 4, 2);

            range1.AddToNamed("TestRange1", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{Headers}";
            ws.Cell(3, 2).Value = "{Data}";
            ws.Cell(4, 2).Value = "{Totals}";

            var panel1 = new ExcelDataSourceDynamicPanel("m:DataProvider:GetDictionaryEnumerable()", ws.NamedRange("TestRange1"), report, report.TemplateProcessor);

            panel1.Render();

            Assert.AreEqual(ws.Range(2, 2, 6, 4), panel1.ResultRange);

            var dictWithDecimalValues = new List <IDictionary <string, decimal> >
            {
                new Dictionary <string, decimal> {
                    ["Value"] = 25.7m
                },
                new Dictionary <string, decimal> {
                    ["Value"] = 250.7m
                },
                new Dictionary <string, decimal> {
                    ["Value"] = 2500.7m
                },
            };

            IXLRange range2 = ws.Range(7, 2, 9, 2);

            range2.AddToNamed("TestRange2", XLScope.Worksheet);

            ws.Cell(7, 2).Value = "{Headers}";
            ws.Cell(8, 2).Value = "{Data}";
            ws.Cell(9, 2).Value = "{Totals}";

            var panel2 = new ExcelDataSourceDynamicPanel(dictWithDecimalValues, ws.NamedRange("TestRange2"), report, report.TemplateProcessor);

            panel2.Render();

            Assert.AreEqual(ws.Range(7, 2, 11, 2), panel2.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourceDynamicPanelDictionaryRenderTest),
                                                                                 nameof(TestRenderDictionaryEnumerable)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
示例#15
0
        public void Test_TwoChildren_Horizontal()
        {
            var          report      = new TestReport();
            IXLWorksheet ws          = report.Workbook.AddWorksheet("Test");
            IXLRange     parentRange = ws.Range(2, 2, 5, 6);

            parentRange.AddToNamed("ParentRange", XLScope.Worksheet);

            IXLRange child1 = ws.Range(2, 4, 5, 4);

            child1.AddToNamed("ChildRange1", XLScope.Worksheet);

            IXLRange child2 = ws.Range(2, 6, 5, 6);

            child2.AddToNamed("ChildRange2", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:Name}";

            ws.Cell(3, 3).Value           = "Field1";
            ws.Cell(3, 3).Style.Font.Bold = true;
            ws.Cell(4, 3).Value           = "Field2";
            ws.Cell(4, 3).Style.Font.Bold = true;
            ws.Cell(3, 4).Value           = "{di:Field1}";
            ws.Cell(4, 4).Value           = "{di:Field2}";
            ws.Cell(5, 5).Value           = "Number";
            ws.Cell(5, 5).Style.Font.Bold = true;
            ws.Cell(5, 6).Value           = "{di:di}";

            var parentPanel = new ExcelDataSourcePanel("m:DataProvider:GetIEnumerable()", ws.NamedRange("ParentRange"), report, report.TemplateProcessor);
            var childPanel1 = new ExcelDataSourcePanel("m:DataProvider:GetChildIEnumerable(di:Name)", ws.NamedRange("ChildRange1"), report, report.TemplateProcessor)
            {
                Parent = parentPanel,
                Type   = PanelType.Horizontal,
            };
            var childPanel2 = new ExcelDataSourcePanel("di:ChildrenPrimitive", ws.NamedRange("ChildRange2"), report, report.TemplateProcessor)
            {
                Parent = parentPanel,
                Type   = PanelType.Horizontal,
            };

            parentPanel.Children = new[] { childPanel1, childPanel2 };
            parentPanel.Render();

            Assert.AreEqual(ws.Range(2, 2, 13, 8), parentPanel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelRender_WithGrouping_MultipleChildrenInOneParent),
                                                                                 nameof(Test_TwoChildren_Horizontal)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void TestMakePanelsHierarchy()
        {
            var          wb = new XLWorkbook();
            IXLWorksheet ws = wb.AddWorksheet("Test");

            IXLRange panel1Range = ws.Range(1, 1, 4, 4);
            IXLRange panel2Range = ws.Range(1, 1, 2, 4);

            panel2Range.AddToNamed("Panel2", XLScope.Worksheet);
            IXLRange panel3Range = ws.Range(2, 1, 2, 4);

            panel3Range.AddToNamed("Panel3", XLScope.Workbook);
            IXLRange panel4Range = ws.Range(5, 1, 6, 5);
            IXLRange panel5Range = ws.Range(6, 1, 6, 5);

            panel5Range.AddToNamed("Panel5", XLScope.Worksheet);
            IXLRange panel6Range = ws.Range(3, 1, 4, 4);
            IXLRange panel7Range = ws.Range(10, 10, 10, 10);
            IXLRange panel8Range = ws.Range(8, 9, 9, 10);

            panel8Range.AddToNamed("Panel8", XLScope.Worksheet);

            var panel1 = new ExcelPanel(panel1Range, new object(), Substitute.For <ITemplateProcessor>());
            var panel2 = new ExcelDataSourcePanel("Stub", ws.NamedRange("Panel2"), new object(),
                                                  Substitute.For <ITemplateProcessor>());
            var panel3 = new ExcelDataSourcePanel("Stub", wb.NamedRange("Panel3"), new object(),
                                                  Substitute.For <ITemplateProcessor>());
            var panel4 = new ExcelPanel(panel4Range, new object(), Substitute.For <ITemplateProcessor>());
            var panel5 = new ExcelDataSourceDynamicPanel("Stub", ws.NamedRange("Panel5"), new object(),
                                                         Substitute.For <ITemplateProcessor>());
            var panel6 = new ExcelPanel(panel6Range, new object(), Substitute.For <ITemplateProcessor>());
            var panel7 = new ExcelPanel(panel7Range, new object(), Substitute.For <ITemplateProcessor>());
            var panel8 = new ExcelTotalsPanel("Stub", ws.NamedRange("Panel8"), new object(),
                                              Substitute.For <ITemplateProcessor>());

            IDictionary <string, (IExcelPanel, string)> panelsFlatView = new Dictionary <string, (IExcelPanel, string)>
            {
示例#17
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);
        }
        public void TestCreateDataSourcePanel()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("d_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("d_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings
            {
                DataSourcePanelPrefix = "d",
                PanelPrefixSeparator  = "_",
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            var panel   = (ExcelDataSourcePanel)factory.Create(namedRange, new Dictionary <string, string>
            {
                [nameof(ExcelDataSourcePanel.Type)]                           = PanelType.Horizontal.ToString(),
                [nameof(ExcelDataSourcePanel.ShiftType)]                      = ShiftType.Row.ToString(),
                [nameof(ExcelDataSourcePanel.RenderPriority)]                 = "5",
                [nameof(ExcelDataSourcePanel.BeforeRenderMethodName)]         = "BeforeRenderMethodName",
                [nameof(ExcelDataSourcePanel.AfterRenderMethodName)]          = "AfterRenderMethodName",
                [nameof(ExcelDataSourcePanel.BeforeDataItemRenderMethodName)] = "BeforeDataItemRenderMethodName",
                [nameof(ExcelDataSourcePanel.AfterDataItemRenderMethodName)]  = "AfterDataItemRenderMethodName",
                ["DataSource"] = "DS",
            });

            Assert.AreEqual(PanelType.Horizontal, panel.Type);
            Assert.AreEqual(ShiftType.Row, panel.ShiftType);
            Assert.AreEqual(5, panel.RenderPriority);
            Assert.AreEqual("BeforeRenderMethodName", panel.BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethodName", panel.AfterRenderMethodName);
            Assert.AreEqual("BeforeDataItemRenderMethodName", panel.BeforeDataItemRenderMethodName);
            Assert.AreEqual("AfterDataItemRenderMethodName", panel.AfterDataItemRenderMethodName);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame("DS", panel.GetType().GetField("_dataSourceTemplate", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));

            ExceptionAssert.Throws <InvalidOperationException>(() => factory.Create(namedRange, null), "Data source panel must have the property \"DataSource\"");
        }
        public void TestCopyIfDataIsSet()
        {
            var          wb                = new XLWorkbook();
            IXLWorksheet ws                = wb.AddWorksheet("Test");
            var          excelReport       = Substitute.For <object>();
            var          templateProcessor = Substitute.For <ITemplateProcessor>();

            IXLRange range = ws.Range(1, 1, 2, 4);

            range.AddToNamed("DataPanel", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("DataPanel");

            object[] data  = { 1, "One" };
            var      panel = new ExcelDataSourcePanel(data, namedRange, excelReport, templateProcessor)
            {
                RenderPriority                 = 10,
                Type                           = PanelType.Horizontal,
                ShiftType                      = ShiftType.NoShift,
                BeforeRenderMethodName         = "BeforeRenderMethod",
                AfterRenderMethodName          = "AfterRenderMethod",
                BeforeDataItemRenderMethodName = "BeforeDataItemRenderMethodName",
                AfterDataItemRenderMethodName  = "AfterDataItemRenderMethodName",
                GroupBy                        = "2,4",
            };

            ExcelDataSourcePanel copiedPanel = (ExcelDataSourcePanel)panel.Copy(ws.Cell(5, 5));

            Assert.AreSame(excelReport, copiedPanel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel));
            Assert.AreSame(templateProcessor, copiedPanel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel));
            Assert.IsNull(copiedPanel.GetType().GetField("_dataSourceTemplate", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel));
            Assert.AreSame(data, copiedPanel.GetType().GetField("_data", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel));
            Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell());
            Assert.AreEqual(ws.Cell(6, 8), copiedPanel.Range.LastCell());
            Assert.AreEqual(10, copiedPanel.RenderPriority);
            Assert.AreEqual(PanelType.Horizontal, copiedPanel.Type);
            Assert.AreEqual(ShiftType.NoShift, copiedPanel.ShiftType);
            Assert.AreEqual("BeforeRenderMethod", copiedPanel.BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethod", copiedPanel.AfterRenderMethodName);
            Assert.AreEqual("BeforeDataItemRenderMethodName", copiedPanel.BeforeDataItemRenderMethodName);
            Assert.AreEqual("AfterDataItemRenderMethodName", copiedPanel.AfterDataItemRenderMethodName);
            Assert.AreEqual("2,4", copiedPanel.GroupBy);
            Assert.IsNull(copiedPanel.Parent);

            //wb.SaveAs("test.xlsx");
        }
        public void TestCreatePanelWithBadName()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("b_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("b_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings {
                PanelPrefixSeparator = "-"
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);

            ExceptionAssert.Throws <InvalidOperationException>(() => factory.Create(namedRange, null), "Panel name \"b_Test\" does not contain prefix separator \"-\"");
        }
        public void TestCreateUnsupportedPanelType()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("b_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("b_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings {
                PanelPrefixSeparator = "_"
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);

            ExceptionAssert.Throws <NotSupportedException>(() => factory.Create(namedRange, null), "Panel type with prefix \"b\" is not supported");
        }
        public void TestRenderIEnumerableOfInt()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 2, 2);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:di}";

            var panel = new ExcelDataSourcePanel(new[] { 1, 2, 3, 4 }, ws.NamedRange("TestRange"), report, report.TemplateProcessor);

            panel.Render();

            Assert.AreEqual(ws.Range(2, 2, 5, 2), panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelIEnumerableRenderTest),
                                                                                 nameof(TestRenderIEnumerableOfInt)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
示例#23
0
        public void TestRenderNullItemHorizontalNoShift()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 3, 5);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:Name}";
            ws.Cell(2, 3).Value = "{di:Date}";
            ws.Cell(2, 4).Value = "{m:Multiply(di:Sum, 5)}";
            ws.Cell(2, 5).Value = "{di:Contacts}";
            ws.Cell(3, 2).Value = "{di:Contacts.Phone}";
            ws.Cell(3, 3).Value = "{di:Contacts.Fax}";
            ws.Cell(3, 4).Value = "{p:StrParam}";

            ws.Cell(1, 1).Value = "{di:Name}";
            ws.Cell(4, 1).Value = "{di:Name}";
            ws.Cell(1, 6).Value = "{di:Name}";
            ws.Cell(4, 6).Value = "{di:Name}";
            ws.Cell(3, 1).Value = "{di:Name}";
            ws.Cell(3, 6).Value = "{di:Name}";
            ws.Cell(1, 4).Value = "{di:Name}";
            ws.Cell(4, 4).Value = "{di:Name}";

            var panel = new ExcelDataSourcePanel("m:DataProvider:GetNullItem()", ws.NamedRange("TestRange"), report, report.TemplateProcessor)
            {
                Type      = PanelType.Horizontal,
                ShiftType = ShiftType.NoShift,
            };

            panel.Render();

            Assert.IsNull(panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelNullItemRenderTest),
                                                                                 nameof(TestRenderNullItemHorizontalNoShift)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        // Rendering speed test
        //[Test]
        public void TestPanelRenderSpeed()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 2, 6);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:Name}";
            ws.Cell(2, 3).Value = "{di:Date}";
            ws.Cell(2, 4).Value = "{di:Sum}";
            ws.Cell(2, 5).Value = "{di:Contacts.Phone}";
            ws.Cell(2, 6).Value = "{di:Contacts.Fax}";

            const int        dataCount = 6000;
            IList <TestItem> data      = new List <TestItem>(dataCount);

            for (int i = 0; i < dataCount; i++)
            {
                data.Add(new TestItem($"Name_{i}", DateTime.Now.AddHours(1), i + 10, new Contacts($"Phone_{i}", $"Fax_{i}")));
            }

            var panel = new ExcelDataSourcePanel(data, ws.NamedRange("TestRange"), report, report.TemplateProcessor)
            {
                //ShiftType = ShiftType.Row,
                //ShiftType = ShiftType.NoShift,
            };

            Stopwatch sw = Stopwatch.StartNew();

            panel.Render();

            sw.Stop();

            //Stopwatch sw2 = Stopwatch.StartNew();

            //report.Workbook.SaveAs("test.xlsx");

            //sw2.Stop();
        }
示例#25
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);
        }
        public void TestPanelRender()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");

            IXLRange range = ws.Range(1, 1, 1, 12);

            range.AddToNamed("Test", XLScope.Worksheet);

            ws.Cell(1, 1).Value     = "Plain text";
            ws.Cell(1, 2).Value     = "{Sum(di:Sum)}";
            ws.Cell(1, 3).Value     = "{ Custom(DI:Sum, CustomAggregation, PostAggregation)  }";
            ws.Cell(1, 4).Value     = "{Min(di:Sum)}";
            ws.Cell(1, 5).Value     = "Text1 {count(di:Name)} Text2 {avg(di:Sum, , PostAggregationRound)} {p:StrParam} {Max(di:Sum)} {Max(di:Sum)}";
            ws.Cell(1, 6).Value     = "{Mix(di:Sum)}";
            ws.Cell(1, 7).FormulaA1 = "=SUM(B1:D1)";
            ws.Cell(1, 8).FormulaA1 = "=ROW()";
            ws.Cell(1, 9).Value     = "{sf:Format(p:DateParam, yyyyMMdd)}";
            ws.Cell(1, 10).Value    = "{p:IntParam}";
            ws.Cell(1, 11).Value    = "{sf:Format(m:TestClassForTotals:Round(Min(di:Sum), 1), #,,0.0000)}";
            ws.Cell(1, 12).Value    = "Aggregation: {m:TestClassForTotals:Meth(avg(di:Sum, , PostAggregationRound), Max( di : Sum ))}. Date: {sf:Format(p:DateParam, dd.MM.yyyy)}";
            ws.Cell(1, 13).Value    = "{Sum(di:Sum)}";

            var panel = new ExcelTotalsPanel("m:DataProvider:GetIEnumerable()", ws.NamedRange("Test"), report, report.TemplateProcessor)
            {
                BeforeRenderMethodName = "TestExcelTotalsPanelBeforeRender",
                AfterRenderMethodName  = "TestExcelTotalsPanelAfterRender",
            };

            panel.Render();

            Assert.AreEqual(range, panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(TotalsPanelRenderTest),
                                                                                 nameof(TestPanelRender)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void TestCancelPanelRender()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 2, 2);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:di}";

            var panel = new ExcelDataSourcePanel(new[] { 1, 2, 3, 4 }, ws.NamedRange("TestRange"), report, report.TemplateProcessor)
            {
                BeforeRenderMethodName = "CancelPanelRender",
            };

            panel.Render();

            Assert.AreEqual(range, panel.ResultRange);

            Assert.AreEqual(1, ws.CellsUsed(XLCellsUsedOptions.Contents).Count());
            Assert.AreEqual("{di:di}", ws.Cell(2, 2).Value);

            //report.Workbook.SaveAs("test.xlsx");
        }
示例#28
0
        public void TestRenderEmptyDataReader()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 4, 2);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{Headers}";
            ws.Cell(2, 2).Style.Border.OutsideBorder      = XLBorderStyleValues.Medium;
            ws.Cell(2, 2).Style.Border.OutsideBorderColor = XLColor.Red;
            ws.Cell(2, 2).Style.Font.Bold = true;

            ws.Cell(3, 2).Value = "{Data}";
            ws.Cell(3, 2).Style.Border.OutsideBorder      = XLBorderStyleValues.Thin;
            ws.Cell(3, 2).Style.Border.OutsideBorderColor = XLColor.Black;

            ws.Cell(4, 2).Value = "{Totals}";
            ws.Cell(4, 2).Style.Border.OutsideBorder      = XLBorderStyleValues.Dotted;
            ws.Cell(4, 2).Style.Border.OutsideBorderColor = XLColor.Green;

            var panel = new ExcelDataSourceDynamicPanel("m:DataProvider:GetEmptyDataReader()", ws.NamedRange("TestRange"), report, report.TemplateProcessor);

            panel.Render();

            Assert.AreEqual(ws.Range(2, 2, 3, 7), panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourceDynamicPanelDataReaderRenderTest),
                                                                                 nameof(TestRenderEmptyDataReader)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void TestRenderDataSet()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range  = ws.Range(2, 2, 2, 6);

            range.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{di:Id}";
            ws.Cell(2, 3).Value = "{di:Name}";
            ws.Cell(2, 4).Value = "{di:IsVip}";
            ws.Cell(2, 5).Value = "{di:Description}";
            ws.Cell(2, 6).Value = "{di:Type}";

            var panel = new ExcelDataSourcePanel("m:DataProvider:GetAllCustomersDataSet()", ws.NamedRange("TestRange"), report, report.TemplateProcessor);

            panel.Render();

            Assert.AreEqual(ws.Range(2, 2, 4, 6), panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourcePanelDataSetRenderTest),
                                                                                 nameof(TestRenderDataSet)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }
        public void TestRenderEmptyEnumerable()
        {
            var          report = new TestReport();
            IXLWorksheet ws     = report.Workbook.AddWorksheet("Test");
            IXLRange     range1 = ws.Range(2, 2, 4, 2);

            range1.AddToNamed("TestRange", XLScope.Worksheet);

            ws.Cell(2, 2).Value = "{Headers}";
            ws.Cell(3, 2).Value = "{Data}";
            ws.Cell(4, 2).Value = "{Totals}";

            var panel = new ExcelDataSourceDynamicPanel("m:DataProvider:GetEmptyIEnumerable()", ws.NamedRange("TestRange"), report, report.TemplateProcessor);

            panel.Render();

            Assert.AreEqual(ws.Range(2, 2, 3, 5), panel.ResultRange);

            ExcelAssert.AreWorkbooksContentEquals(TestHelper.GetExpectedWorkbook(nameof(DataSourceDynamicPanelEnumerableRenderTest),
                                                                                 nameof(TestRenderEmptyEnumerable)), ws.Workbook);

            //report.Workbook.SaveAs("test.xlsx");
        }