Beispiel #1
0
        internal ExcelTemplateDefinition GetTemplateDefinitionFromLink(ExcelTemplateDefinitionPart parent, TemplateLink templateLink)
        {
            try
            {
                string[] tos = templateLink.To.Split('.');
                ExcelInterop.Worksheet sheetContainer = null;
                string templateName;
                if (tos.Count() == 1)
                {
                    sheetContainer = parent.DefinitionCells.Worksheet;
                    templateName   = tos[0].EmptyIfNull().Trim();
                }
                else
                {
                    string worksheetContainerName = tos[0].EmptyIfNull().Trim();
                    templateName = tos[1].EmptyIfNull().Trim();

                    ExcelInterop.Worksheet parentWorkSheet = parent.DefinitionCells.Worksheet;
                    ExcelInterop.Workbook  workbook        = parentWorkSheet.Parent as ExcelInterop.Workbook;
                    if (workbook == null)
                    {
                        throw new EtkException("Cannot retrieve the workbook that owned the template destination sheet");
                    }

                    List <ExcelInterop.Worksheet> sheets = new List <ExcelInterop.Worksheet>(workbook.Worksheets.Cast <ExcelInterop.Worksheet>());
                    sheetContainer = sheets.FirstOrDefault(s => !string.IsNullOrEmpty(s.Name) && s.Name.Equals(worksheetContainerName));
                    if (sheetContainer == null)
                    {
                        throw new EtkException($"Cannot find the sheet '{worksheetContainerName}' in the current workbook", false);
                    }

                    ExcelApplication.ReleaseComObject(workbook);
                    workbook = null;
                }

                string templateDescriptionKey = $"{sheetContainer.Name}-{templateName}";
                ExcelTemplateDefinition templateDefinition = bindingTemplateManager.GetTemplateDefinition(templateDescriptionKey) as ExcelTemplateDefinition;
                if (templateDefinition == null)
                {
                    ExcelInterop.Range range = sheetContainer.Cells.Find(string.Format(TEMPLATE_START_FORMAT, templateName), Type.Missing, ExcelInterop.XlFindLookIn.xlValues, ExcelInterop.XlLookAt.xlPart, ExcelInterop.XlSearchOrder.xlByRows, ExcelInterop.XlSearchDirection.xlNext, false);
                    if (range == null)
                    {
                        throw new EtkException($"Cannot find the template '{templateName.EmptyIfNull()}' in sheet '{sheetContainer.Name.EmptyIfNull()}'");
                    }
                    templateDefinition = ExcelTemplateDefinitionFactory.CreateInstance(templateName, range);
                    bindingTemplateManager.RegisterTemplateDefinition(templateDefinition);

                    range = null;
                }

                ExcelApplication.ReleaseComObject(sheetContainer);
                sheetContainer = null;
                return(templateDefinition);
            }
            catch (Exception ex)
            {
                string message = $"Cannot create the template dataAccessor. {ex.Message}";
                throw new EtkException(message, false);
            }
        }
Beispiel #2
0
        private ExcelTemplateView CreateView(ExcelInterop.Worksheet sheetContainer, string templateName, ExcelInterop.Worksheet sheetDestination, ExcelInterop.Range firstOutputCell, ExcelInterop.Range clearingCell)
        {
            if (sheetContainer == null)
            {
                throw new ArgumentNullException("Template container sheet is mandatory");
            }
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException("Template name is mandatory");
            }
            if (sheetDestination == null)
            {
                throw new ArgumentNullException("Template destination sheet is mandatory");
            }
            if (firstOutputCell == null)
            {
                throw new ArgumentNullException("Template first output cell is mandatory");
            }

            if (clearingCell == null)
            {
                try
                {
                    clearingCell = sheetContainer.Cells[1, 1];
                }
                catch
                {
                    throw new ArgumentException("The clearing cell value");
                }
            }

            ExcelInterop.Range range = sheetContainer.Cells.Find(string.Format(TEMPLATE_START_FORMAT, templateName), Type.Missing, ExcelInterop.XlFindLookIn.xlValues, ExcelInterop.XlLookAt.xlPart, ExcelInterop.XlSearchOrder.xlByRows, ExcelInterop.XlSearchDirection.xlNext, false);
            if (range == null)
            {
                throw new EtkException($"Cannot find the template '{templateName.EmptyIfNull()}' in sheet '{sheetContainer.Name.EmptyIfNull()}'");
            }

            string             templateDescriptionKey = $"{sheetContainer.Name}-{templateName}";
            TemplateDefinition templateDefinition     = bindingTemplateManager.GetTemplateDefinition(templateDescriptionKey);

            if (templateDefinition == null)
            {
                templateDefinition = ExcelTemplateDefinitionFactory.CreateInstance(templateName, range);
                bindingTemplateManager.RegisterTemplateDefinition(templateDefinition);
            }

            ExcelTemplateView view = new ExcelTemplateView(templateDefinition, sheetDestination, firstOutputCell, clearingCell);

            bindingTemplateManager.AddView(view);
            log.LogFormat(LogType.Debug, "Sheet '{0}', View '{1}'.'{2}' created.", sheetDestination.Name.EmptyIfNull(), sheetContainer.Name.EmptyIfNull(), templateName.EmptyIfNull());
            range = null;

            return(view);
        }