예제 #1
0
        /// <summary>
        ///     Get sheet info from the imported spreadsheet.
        /// </summary>
        /// <param name="fileUploadId">File upload Id.</param>
        /// <param name="sheetId">Selected sheet info.</param>
        /// <param name="headerRowNo">Header row no.</param>
        /// <param name="dataRowNo">Data row number to start reading data.</param>
        /// <param name="lastRowNo">Optional last row number to read.</param>
        /// <param name="fileFormat">Imported file fileFormat ( Excel or CSV)</param>
        /// <returns>
        ///     Sheet Info.
        /// </returns>
        public SampleTable GetSampleTable(string fileUploadId, ImportFormat fileFormat, string sheetId, int headerRowNo, int dataRowNo, int?lastRowNo)
        {
            // Get service
            IDataFileReaderService service = _readerActivator(fileFormat);

            // Settings
            DataFileReaderSettings settings = new DataFileReaderSettings
            {
                ImportFormat       = fileFormat,
                HeadingRowNumber   = headerRowNo,
                FirstDataRowNumber = dataRowNo,
                LastDataRowNumber  = lastRowNo,
                SheetId            = sheetId
            };

            // Open stream
            using (Stream stream = FileRepositoryHelper.GetTemporaryFileDataStream(fileUploadId))
            {
                // Build sample
                SampleDataCreator creator     = new SampleDataCreator( );
                SampleTable       sampleTable = creator.CreateSample(stream, settings, service);

                // Trim titles
                foreach (var col in sampleTable.Columns)
                {
                    col.Name = col.Name?.Trim( ) ?? "";
                }

                return(sampleTable);
            }
        }
        /// <summary>
        ///     Packs the spreadsheet information response.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        private static SampleTable PackSampleTable(Interface.SampleTable sampleTable)
        {
            SampleTable result = new SampleTable
            {
                Rows    = sampleTable.Rows.Select(row => PackSampleRow(row)).ToList(),
                Columns = sampleTable.Columns.Select(column => PackSampleColumn(column)).ToList( ),
            };

            return(result);
        }
예제 #3
0
        /// <summary>
        /// Read sample data from a spreadsheet reader.
        /// </summary>
        /// <param name="stream">The spreadsheet.</param>
        /// <param name="settings">Settings.</param>
        /// <param name="service">The reader.</param>
        /// <returns></returns>
        public SampleTable CreateSample(Stream stream, DataFileReaderSettings settings, IDataFileReaderService service)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            // Read field list
            using (IDataFile dataFile = service.OpenDataFile(stream, settings))
            {
                SheetMetadata metadata = dataFile.ReadMetadata( );

                SampleTable table = new SampleTable
                {
                    Columns = metadata.Fields.Select(
                        field =>
                        new SampleColumn
                    {
                        ColumnName = field.Key,
                        Name       = field.Title
                    }
                        ).ToList( )
                };

                // Read records
                var records = dataFile.GetObjects( ).Take(NumberOfSampleRows);

                // Convert to sample rows
                List <SampleRow> sampleRows = new List <SampleRow>( );

                foreach (IObjectReader record in records)
                {
                    // Read values
                    var values = metadata.Fields.Select(field =>
                    {
                        try
                        {
                            return(record.GetString(field.Key));
                        }
                        catch
                        {
                            return(string.Empty);
                        }
                    }).ToList( );

                    // Create sample row
                    SampleRow row = new SampleRow
                    {
                        Values = values
                    };
                    sampleRows.Add(row);
                }

                table.Rows = sampleRows;
                return(table);
            }
        }