/// <summary> /// Method that generates an CSV file with species observations. /// </summary> /// <param name="presentationCoordinateSystem">Coordinate system to use.</param> /// <param name="columnsSet">Which columns set to use.</param> /// <param name="columnsHeadersType">Columns headers type to use.</param> /// <returns>A CSV file.</returns> public FileResult SpeciesObservationsAsCsv( int?presentationCoordinateSystem, int?columnsSet, int?columnsHeadersType) { if (NoFilterSelected) { throw new Exception("Too much data! You must set taxa filter or spatial filter."); } var coordinateSystemId = GetCoordinateSystemIdFromArgument( presentationCoordinateSystem, SessionHandler.MySettings.Presentation.Map.DownloadCoordinateSystemId); SpeciesObservationTableColumnsSetId speciesObservationTableColumnsSetId = GetSpeciesObservationTableColumnsSetIdFromArgument( columnsSet, SessionHandler.MySettings.Presentation.Table.SpeciesObservationTable.SpeciesObservationTableColumnsSetId); bool useLabelAsColumnHeader = GetUselabelAsColumnHeaderFromArgument( columnsHeadersType, SessionHandler.MySettings.Presentation.Table.SpeciesObservationTable.UseLabelAsColumnHeader); SpeciesObservationResultCalculator resultCalculator = new SpeciesObservationResultCalculator(GetCurrentUser(), SessionHandler.MySettings); List <Dictionary <ViewTableField, string> > result = resultCalculator.GetTableResult( coordinateSystemId, speciesObservationTableColumnsSetId); SpeciesObservationsCsv file = FileExportManager.GetSpeciesObservationsAsCsv( result, useLabelAsColumnHeader); MemoryStream returnStream = file.ToStream(); SetServerDone(); return(File( returnStream.ToArray(), "text/csv", FilenameGenerator.CreateFilename("SpeciesObservations", FileType.Csv))); }
/// <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 SpeciesObservationResultCalculator(currentUser, SessionHandler.MySettings); List <Dictionary <ViewTableField, string> > speciesObservations = resultCalculator.GetTableResult(this.coordinateSystemId, this.speciesObservationTableColumnsSetId); // 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, speciesObservations); AddSpeciesObservationsData(worksheet, speciesObservations); FormatHeader(worksheet, 1, speciesObservations.Any() ? speciesObservations.First().Keys.Count : 1); 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; } }
/// <summary> /// Initializes a new instance of the <see cref="SpeciesObservationsExcelXml"/> class. /// </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> /// <param name="coordinateSystemId">The coordinate system.</param> /// <param name="speciesObservationTableColumnsSetId">The table columns set to use.</param> /// <param name="useLabelAsColumnHeader">Use label as column header.</param> public SpeciesObservationsExcelXml( IUserContext currentUser, bool addSettings, bool addProvenance, CoordinateSystemId coordinateSystemId, SpeciesObservationTableColumnsSetId speciesObservationTableColumnsSetId, bool useLabelAsColumnHeader) { var resultCalculator = new SpeciesObservationResultCalculator(currentUser, SessionHandler.MySettings); var speciesObservations = resultCalculator.GetTableResult(coordinateSystemId, speciesObservationTableColumnsSetId); _xmlBuilder = new StringBuilder(); // Add file definitions and basic format settings _xmlBuilder.AppendLine(GetInitialSection()); // Specify column and row counts List <string> columns = new List <string>(); if (speciesObservations.Count > 0) { foreach (ViewTableField tableField in speciesObservations[0].Keys) { if (useLabelAsColumnHeader) { columns.Add(tableField.Title); } else { columns.Add(tableField.DataField); } } } _xmlBuilder.AppendLine(GetColumnInitialSection(columns.Count, speciesObservations.Count)); // Specify column widths foreach (string column in columns) { _xmlBuilder.AppendLine(GetColumnWidthLine(100)); } // Add row with column headers _xmlBuilder.AppendLine(GetRowStart()); foreach (string column in columns) { _xmlBuilder.AppendLine(GetColumnNameRowLine(column)); } _xmlBuilder.AppendLine(GetRowEnd()); // Data values foreach (Dictionary <ViewTableField, string> speciesObservation in speciesObservations) { _xmlBuilder.AppendLine(GetRowStart()); foreach (var val in speciesObservation.Values) { _xmlBuilder.AppendLine(GetDataRowLine("String", val)); } _xmlBuilder.AppendLine(GetRowEnd()); } _xmlBuilder.AppendLine(GetFinalSection(GetAditionalSheets(currentUser, addSettings, addProvenance))); }