public void Должен_заменить_маркер_на_текст()
        {
            //assign
            var excelHelper  = new ExcelHelper();
            var templatePath = "Assets/Templates/one-marker.xlsx";

            using var workbook = new XLWorkbook(templatePath);
            var injection = new TextInjection {
                Resource = new TextResourceObject("text")
            };
            var documentInjectorOptions = new DocumentInjectorOptions
            {
                ResourceInjector  = new VariantResourceInjector(),
                InjectionProvider = new FuncInjectionProvider(_ => injection),
                MarkerOptions     = new MarkerOptions("{", ".", "}"),
            };
            var documentInjector = new DocumentInjector(documentInjectorOptions);

            //act
            documentInjector.Inject(workbook);

            //assert
            var values = excelHelper.ReadCellRangeValues(workbook, (1, 1, 1), (1, 1, 1));

            values[0][0].Should().Be("text");
        }
        public void Должен_вставить_изображение_и_удалить_маркер_из_документа()
        {
            //assign
            var excelHelper = new ExcelHelper();

            using var workbook = new XLWorkbook("Assets/Templates/one-marker.xlsx");
            var imageBytes = File.ReadAllBytes("Assets/Images/checker.png");
            var injection  = new ImageInjection {
                Resource = new ImageResourceObject(imageBytes)
            };
            var documentInjectorOptions = new DocumentInjectorOptions
            {
                ResourceInjector  = new VariantResourceInjector(),
                InjectionProvider = new FuncInjectionProvider(_ => injection),
                MarkerOptions     = new MarkerOptions("{", ".", "}"),
            };
            var documentInjector = new DocumentInjector(documentInjectorOptions);

            //act
            documentInjector.Inject(workbook);

            //assert
            var values = excelHelper.ReadCellRangeValues(workbook, (1, 1, 1), (1, 1, 1));

            values[0][0].Should().Be(""); //проверяем что маркер удален из документа
        }
Esempio n. 3
0
        public TemplateBuilder InjectData(DocumentInjectorOptions options)
        {
            var documentInjector = new DocumentInjector(options);

            documentInjector.Inject(_workbook);

            return(this);
        }
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            var templates = new[]
            {
                //"template1",
                "Данные для расчета КП 1 (1)",
            };
            var files = templates
                        .Select(x => new
            {
                In  = $"./Templates/{x}.xlsx",
                Out = $"./Output/{x}.out.xlsx"
            })
                        .ToList();

            files.ForEach(file =>
            {
                Console.WriteLine($"workbook: {file}");
                using var fileStream = File.Open(file.In, FileMode.Open, FileAccess.Read);

                var templateBuilder = new TemplateBuilder(fileStream);
                var markerOptions   = new MarkerOptions("{{", ".", "}}");

                //при реальном использование есть необходимость извлечь все маркеры прежде чем двигаться дальше
                //маркеры необходимы для того что бы отправить запрос за данными
                var allMarkers = templateBuilder.ReadMarkers(markerOptions);
                Console.WriteLine($"Found {allMarkers.Count}: {string.Join(',', allMarkers.Select(x => x.Id))}");

                var resourceInjector        = new ResourceInjector();
                var injectionProvider       = new InjectionProvider();
                var documentInjectorOptions = new DocumentInjectorOptions
                {
                    ResourceInjector  = resourceInjector,
                    InjectionProvider = injectionProvider,
                    MarkerOptions     = markerOptions,
                };

                var documentStream = templateBuilder
                                     .InjectData(documentInjectorOptions)
                                     .SetupFormulaCalculations(new FormulaCalculationOptions {
                    ForceFullCalculation = true, FullCalculationOnLoad = true
                })
                                     .RecalculateFormulasOnBuild(false)
                                     .Build(false);

                using (var outputFileStream = File.Open(file.Out, FileMode.Create, FileAccess.ReadWrite))
                    documentStream.CopyTo(outputFileStream);
            });

            //Console.ReadKey();
        }
        public void Должен_заменить_маркер_на_таблицу()
        {
            //assign
            var excelHelper  = new ExcelHelper();
            var templatePath = "Assets/Templates/one-marker.xlsx";

            using var workbook = new XLWorkbook(templatePath);
            var resourceObject = new TableResourceObject(new List <List <object> > {
                new List <object> {
                    1, 2
                },
                new List <object> {
                    3, 4
                },
            });
            var injection = new TableInjection {
                Resource = resourceObject, LayoutShift = LayoutShiftType.None
            };
            var documentInjectorOptions = new DocumentInjectorOptions
            {
                ResourceInjector  = new VariantResourceInjector(),
                InjectionProvider = new FuncInjectionProvider(_ => injection),
                MarkerOptions     = new MarkerOptions("{", ".", "}"),
            };
            var documentInjector = new DocumentInjector(documentInjectorOptions);

            //act
            documentInjector.Inject(workbook);

            //assert
            var values = excelHelper.ReadCellRangeValues(workbook, (1, 1, 1), (2, 2, 2));

            values[0][0].Should().Be(1);
            values[0][1].Should().Be(2);
            values[1][0].Should().Be(3);
            values[1][1].Should().Be(4);
        }
 public DocumentInjector(DocumentInjectorOptions options)
 {
     _resourceInjector  = options.ResourceInjector;
     _injectionProvider = options.InjectionProvider;
     _markerOptions     = options.MarkerOptions;
 }