public static IDataTable FromFile(
            string fileName,
            Stream stream,
            long contentLength,
            CsvConfiguration configuration,
            int skip = 0,
            int take = int.MaxValue)
        {
            Guard.ArgumentNotEmpty(() => fileName);
            Guard.ArgumentNotNull(() => stream);
            Guard.ArgumentNotNull(() => configuration);

            if (contentLength == 0)
            {
                throw Error.Argument("fileName", "The posted file '{0}' does not contain any data.".FormatInvariant(fileName));
            }

            IDataReader dataReader = null;

            try
            {
                var fileExt = System.IO.Path.GetExtension(fileName).ToLowerInvariant();

                switch (fileExt)
                {
                case ".xlsx":
                    dataReader = new ExcelDataReader(stream, true);                             // TODO: let the user specify if excel file has headers
                    break;

                default:
                    dataReader = new CsvDataReader(new StreamReader(stream), configuration);
                    break;
                }

                var table = LightweightDataTable.FromDataReader(dataReader, skip, take);

                if (table.Columns.Count == 0 || table.Rows.Count == 0)
                {
                    throw Error.InvalidOperation("The posted file '{0}' does not contain any columns or data rows.".FormatInvariant(fileName));
                }

                return(table);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Dispose();
                    }
                    dataReader = null;
                }
            }
        }
        public static IDataTable FromDataReader(
            IDataReader reader,
            int skip = 0,
            int take = int.MaxValue)
        {
            Guard.ArgumentNotNull(() => reader);

            if (reader.IsClosed)
            {
                throw new ArgumentException("This operation is invalid when the reader is closed.", "reader");
            }

            var columns = new List <IDataColumn>(reader.FieldCount);
            var data    = new List <object[]>();

            var schema = reader.GetSchemaTable();

            var nameCol = schema.Columns[SchemaTableColumn.ColumnName];
            var typeCol = schema.Columns[SchemaTableColumn.DataType];

            foreach (DataRow schemaRow in schema.Rows)
            {
                var column = new LightweightDataColumn((string)schemaRow[nameCol], (Type)schemaRow[typeCol]);
                columns.Add(column);
            }

            var fieldCount = reader.FieldCount;

            take = Math.Min(take, int.MaxValue - skip);

            int i = -1;

            while (reader.Read())
            {
                i++;

                if (skip > i)
                {
                    continue;
                }

                if (i >= skip + take)
                {
                    break;
                }

                var values = new object[fieldCount];
                reader.GetValues(values);
                data.Add(values);
            }

            var table = new LightweightDataTable(columns, data);

            return(table);
        }
Exemplo n.º 3
0
        private void ImportCoreInner(DataImporterContext ctx, string filePath)
        {
            if (ctx.ExecuteContext.Abort == DataExchangeAbortion.Hard)
            {
                return;
            }

            {
                var logHead = new StringBuilder();
                logHead.AppendLine();
                logHead.AppendLine(new string('-', 40));
                logHead.AppendLine("cloudCommerce.NET:\t\tv." + cloudCommerceVersion.CurrentFullVersion);
                logHead.Append("Import profile:\t\t" + ctx.Request.Profile.Name);
                logHead.AppendLine(ctx.Request.Profile.Id == 0 ? " (volatile)" : " (Id {0})".FormatInvariant(ctx.Request.Profile.Id));

                logHead.AppendLine("Entity:\t\t\t" + ctx.Request.Profile.EntityType.ToString());
                logHead.AppendLine("File:\t\t\t" + Path.GetFileName(filePath));

                var customer = _services.WorkContext.CurrentCustomer;
                logHead.Append("Executed by:\t\t" + (customer.Email.HasValue() ? customer.Email : customer.SystemName));

                ctx.Log.Information(logHead.ToString());
            }

            if (!File.Exists(filePath))
            {
                throw new SmartException("File does not exist {0}.".FormatInvariant(filePath));
            }

            CsvConfiguration csvConfiguration = null;
            var extension = Path.GetExtension(filePath);

            if ((new string[] { ".csv", ".txt", ".tab" }).Contains(extension, StringComparer.OrdinalIgnoreCase))
            {
                var converter = new CsvConfigurationConverter();
                csvConfiguration = converter.ConvertFrom <CsvConfiguration>(ctx.Request.Profile.FileTypeConfiguration);
            }

            if (csvConfiguration == null)
            {
                csvConfiguration = CsvConfiguration.ExcelFriendlyConfiguration;
            }

            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                ctx.ExecuteContext.DataTable = LightweightDataTable.FromFile(
                    Path.GetFileName(filePath),
                    stream,
                    stream.Length,
                    csvConfiguration,
                    ctx.Request.Profile.Skip,
                    ctx.Request.Profile.Take > 0 ? ctx.Request.Profile.Take : int.MaxValue
                    );

                try
                {
                    ctx.Importer.Execute(ctx.ExecuteContext);
                }
                catch (Exception exception)
                {
                    ctx.ExecuteContext.Abort = DataExchangeAbortion.Hard;
                    ctx.ExecuteContext.Result.AddError(exception, "The importer failed: {0}.".FormatInvariant(exception.ToAllMessages()));
                }

                if (ctx.ExecuteContext.IsMaxFailures)
                {
                    ctx.ExecuteContext.Result.AddWarning("Import aborted. The maximum number of failures has been reached.");
                }

                if (ctx.CancellationToken.IsCancellationRequested)
                {
                    ctx.ExecuteContext.Result.AddWarning("Import aborted. A cancellation has been requested.");
                }
            }
        }