Example #1
0
        /// <summary>
        /// Creates a SpreadSheetDocument from the specified
        /// template
        /// </summary>
        /// <param name="template">Template url</param>
        /// <returns>SpreadSheetDocument reference</returns>
        public static SpreadsheetDocument ExcelDocument(string template = null)
        {
            template ??= $@"{AppDomain.CurrentDomain.BaseDirectory}Extensions\Excel\Templates\Report.xlsx";
            if (!template.IsFilePath(out Uri path))
            {
                return(null);
            }
            //Initialize via report template
            SpreadsheetDocument document = SpreadsheetDocument.CreateFromTemplate(template);

            //Compress using maximum value possible
            document.CompressionOption = CompressionOption.Maximum;
            //Bond the workbook to the document or else create a new workspace
            WorkbookPart book = document.WorkbookPart ?? document.AddWorkbookPart();
            //Initialize workspace
            WorksheetPart workSpace = document.WorkbookPart.WorksheetParts.FirstOrDefault() ?? document.WorkbookPart.AddNewPart <WorksheetPart>();

            //Initialize worksheet
            if (workSpace.Worksheet == null)
            {
                workSpace.Worksheet = new Worksheet();
            }
            //Load default style sheet
            document.LoadDefaultStyleSheet();
            //Share string table part
            StringPart = book.GetPartsOfType <SharedStringTablePart>()
                         .FirstOrDefault();
            TableStrings = StringPart.SharedStringTable
                           .Select(s => s.InnerText)
                           .ToList();
            //Add Document properties
            document.SetProperties(@"Reporte");
            //Add extended Document properties
            document.SetExtendedProperties(null, null);
            //Save all changes
            document.Save();
            return(document);
        }