private object ImportText() { var fileName = Project.Current.MapPath(FileName); if (string.IsNullOrWhiteSpace(fileName) || !File.Exists(fileName)) { throw new FileNotFoundException($"File '{fileName}' not found."); } var schema = new FixedLengthSchema(); if (Columns != null) { foreach (var column in Columns) { var definition = CreateColumnDefinition(column); var window = new Window(column.ColumnLength); if (column.FillCharacter.HasValue) { window.FillCharacter = column.FillCharacter.Value; } if (column.Alignment.HasValue) { window.Alignment = column.Alignment.Value; } if (column.TruncationPolicy.HasValue) { window.TruncationPolicy = column.TruncationPolicy.Value; } schema.AddColumn(definition, window); } } #pragma warning disable IDE0017 // Simplify object initialization var options = new FixedLengthOptions(); #pragma warning restore IDE0017 // Simplify object initialization options.FillCharacter = FillCharacter; options.HasRecordSeparator = !NoRecordSeparator; if (RecordSeparator != null) { options.RecordSeparator = RecordSeparator; } options.IsFirstRecordHeader = !NoHeaderRow; options.Alignment = Alignment; if (!string.IsNullOrWhiteSpace(Culture)) { options.FormatProvider = new CultureInfo(Culture); } var readerOptions = new FlatFileDataReaderOptions() { IsDBNullReturned = true, IsNullStringAllowed = true }; using var reader = new StreamReader(File.OpenRead(fileName)); var csvReader = new FixedLengthReader(reader, schema, options); var dataReader = new FlatFileDataReader(csvReader, readerOptions); var resultReader = new DataReaderWrapper(dataReader, new DataReaderWrapper.DataReaderWrapperParameters() { Columns = this.SelectColumns, SkipColumns = this.SkipColumns, IgnoreReaderErrors = this.IgnoreReaderErrors, CloseAction = () => { reader.Dispose(); } }); if (AsDataReader) { return(resultReader); } else { var table = new DataTable("TextData"); table.Load(resultReader); return(table); } }
private object ImportText() { var fileName = Project.Current.MapPath(FileName); if (string.IsNullOrWhiteSpace(fileName) || !File.Exists(fileName)) { throw new FileNotFoundException($"File '{fileName}' not found."); } if (Columns == null && NoHeaderRow) { throw new Exception($"{nameof(NoHeaderRow)} requires providing {nameof(Columns)} schema."); } var schema = new SeparatedValueSchema(); if (Columns != null) { foreach (var column in Columns) { var definition = CreateColumnDefinition(column); schema.AddColumn(definition); } } var options = new SeparatedValueOptions(); if (Separator != null) { options.Separator = Separator; } if (RecordSeparator != null) { options.RecordSeparator = RecordSeparator; } options.Quote = Quote; options.IsFirstRecordSchema = !NoHeaderRow; options.PreserveWhiteSpace = PreserveWhiteSpace; if (!string.IsNullOrWhiteSpace(Culture)) { options.FormatProvider = new CultureInfo(Culture); } var readerOptions = new FlatFileDataReaderOptions() { IsDBNullReturned = true, IsNullStringAllowed = true }; var reader = new StreamReader(File.OpenRead(fileName)); var csvReader = Columns != null ? new SeparatedValueReader(reader, schema, options) : new SeparatedValueReader(reader, options); var dataReader = new FlatFileDataReader(csvReader, readerOptions); var resultReader = new DataReaderWrapper(dataReader, new DataReaderWrapper.DataReaderWrapperParameters() { Columns = this.SelectColumns, SkipColumns = this.SkipColumns, IgnoreReaderErrors = this.IgnoreReaderErrors, CloseAction = () => { reader.Dispose(); } }); if (AsDataReader) { return(resultReader); } else { var table = new DataTable("TextData"); table.Load(resultReader); return(table); } }