Beispiel #1
0
        /// <summary>
        /// Constructor of an excel xml file with summary statistics.
        /// </summary>
        /// <param name="currentUser">Current user context</param>
        /// <param name="addSettings">True if settings sheet should be included</param>
        /// <param name="addProvenance">True if provenance sheet should be included.</param>
        public ObservedTaxonCountListAsExcelXml(IUserContext currentUser, bool addSettings, bool addProvenance)
            : base()
        {
            var resultCalculator = new SpeciesObservationTaxonSpeciesObservationCountTableResultCalculator(currentUser, SessionHandler.MySettings);
            var data             = resultCalculator.GetResultFromCacheIfAvailableOrElseCalculate();

            _xmlBuilder = new StringBuilder();

            // Add file definitions and basic format settings
            _xmlBuilder.AppendLine(GetInitialSection());

            // Specify column and row counts
            _xmlBuilder.AppendLine(GetColumnInitialSection(8, data.Count));

            // Specify column widths
            _xmlBuilder.AppendLine(GetColumnWidthLine(200));
            _xmlBuilder.AppendLine(GetColumnWidthLine(200));
            _xmlBuilder.AppendLine(GetColumnWidthLine(150));
            _xmlBuilder.AppendLine(GetColumnWidthLine(90));
            _xmlBuilder.AppendLine(GetColumnWidthLine(90));
            _xmlBuilder.AppendLine(GetColumnWidthLine(60));
            _xmlBuilder.AppendLine(GetColumnWidthLine(90));
            _xmlBuilder.AppendLine(GetColumnWidthLine(90));

            // Add row with column headers
            _xmlBuilder.AppendLine(GetRowStart());
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.LabelTaxon));
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.LabelAuthor));
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.LabelSwedishName));
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.LabelCategory));
            _xmlBuilder.AppendLine(GetColumnNameRowLine("Status"));
            _xmlBuilder.AppendLine(GetColumnNameRowLine("Dyntaxa info"));
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.LabelTaxonId));
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.LabelSpeciesObservationCount));
            _xmlBuilder.AppendLine(GetRowEnd());

            // Data values
            foreach (TaxonSpeciesObservationCountViewModel row in data)
            {
                _xmlBuilder.AppendLine(GetRowStart());
                _xmlBuilder.AppendLine(GetDataRowLine("String", row.ScientificName));
                _xmlBuilder.AppendLine(GetDataRowLine("String", row.Author));
                _xmlBuilder.AppendLine(GetDataRowLine("String", row.CommonName));
                _xmlBuilder.AppendLine(GetDataRowLine("String", row.Category));
                _xmlBuilder.AppendLine(GetDataRowLine("String", row.TaxonStatus.ToString()));
                _xmlBuilder.AppendLine(GetDataRowLine("String", @"Http://Dyntaxa.se/Taxon/Info/" + row.TaxonId.ToString()));
                _xmlBuilder.AppendLine(GetDataRowLine("Number", row.TaxonId.ToString()));
                _xmlBuilder.AppendLine(GetDataRowLine("Number", row.SpeciesObservationCount.ToString()));
                _xmlBuilder.AppendLine(GetRowEnd());
            }

            // Add final section of the xml document.
            _xmlBuilder.AppendLine(GetFinalSection(GetAditionalSheets(currentUser, addSettings, addProvenance)));
            _xmlBuilder.Replace("&", "&amp;");
        }
        /// <summary>
        /// Creates an excel file.
        /// Writes the content of a list into a worksheet of an excelfile and save the file.
        /// </summary>
        /// <param name="autosizeColumnWidth">
        /// If true, the columns will be autosized.
        /// </param>
        /// <returns>
        /// The <see cref="MemoryStream"/>.
        /// </returns>
        private MemoryStream CreateExcelFile(bool autosizeColumnWidth = false)
        {
            var memoryStream = new MemoryStream();

            try
            {
                using (ExcelPackage package = new ExcelPackage(memoryStream))
                {
                    var resultCalculator = new SpeciesObservationTaxonSpeciesObservationCountTableResultCalculator(currentUser, SessionHandler.MySettings);
                    var data             = resultCalculator.GetResultFromCacheIfAvailableOrElseCalculate();
                    // Add a new worksheet to the empty workbook.
                    // The name of the sheet can not be longer than 31 characters.
                    var worksheet = package.Workbook.Worksheets.Add("SLW Data");
                    AddHeaders(worksheet);
                    AddContentData(worksheet, data);
                    FormatHeader(worksheet, 1, 8);

                    if (autosizeColumnWidth)
                    {
                        worksheet.Cells.AutoFitColumns(0);
                    }

                    //Add aditional sheets if user has request that
                    AddAditionalSheets(package);

                    package.Save();
                }

                memoryStream.Position = 0;
                return(memoryStream);
            }
            catch (Exception)
            {
                memoryStream.Dispose();

                throw;
            }
        }