Exemplo n.º 1
0
        private DataTable ReadData(TxtImportSettings settings, int maxRowsCount)
        {
            var fileName = settings.PathToFile;

            DataTable result;

            if (!File.Exists(fileName))
            {
                result = new DataTable();
            }
            else
            {
                switch (settings.FileType)
                {
                case TxtFileType.Delimited:
                    result = GetPreviewForDelimitedFile(settings, maxRowsCount);
                    break;

                case TxtFileType.FixedWidth:
                    result = GetPreviewForFixedWidthFile(settings, maxRowsCount);
                    break;

                default:
                    result = new DataTable();
                    break;
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        private static DataTable GetPreviewForDelimitedFile(TxtImportSettings settings, int maxRowsCount)
        {
            var fileName = settings.PathToFile;

            var result = new DataTable();

            using (var streamReader = new StreamReader(fileName))
            {
                while (streamReader.Peek() != -1 &&
                       (maxRowsCount < 0 ||
                        result.Rows.Count < maxRowsCount))
                {
                    var line = streamReader.ReadLine();
                    if (String.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    var splitValues = line.Split(new[] { settings.Delimiter }, StringSplitOptions.RemoveEmptyEntries);
                    if (splitValues.Length == 0)
                    {
                        continue;
                    }

                    if (result.Columns.Count == 0)
                    {
                        foreach (var splitValue in splitValues)
                        {
                            result.Columns.Add(splitValue);
                        }
                    }
                    else
                    {
                        var row       = result.NewRow();
                        var minColumn = Math.Min(splitValues.Length, result.Columns.Count);
                        for (int i = 0; i < minColumn; i++)
                        {
                            row[i] = splitValues[i];
                        }
                        result.Rows.Add(row);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public FormatOptionsPage(WizardContext context)
        {
            _settings = (TxtImportSettings)context.Settings;
            _importer = context.Importer;

            InitializeComponent();

            // FileType combo box
            cmbFileType.DataSource = Enum.GetValues(typeof(TxtFileType));
            cmbFileType.Format    += delegate(object s, ListControlConvertEventArgs args)
            {
                args.Value = ((TxtFileType)args.ListItem).Description();
            };
            cmbFileType.SelectedValueChanged += CmbFileTypeOnSelectedValueChanged;
            cmbFileType.DataBindings.Clear();
            cmbFileType.DataBindings.Add("SelectedItem", _settings, "FileType", true, DataSourceUpdateMode.OnPropertyChanged);

            // Delimiter
            _settings.Delimiter = delimiterSelector.CurrentDelimiter;
            delimiterSelector.CurrentDelimiterChanged += DelimiterSelectorOnCurrentDelimiterChanged;

            //
            CmbFileTypeOnSelectedValueChanged(cmbFileType, EventArgs.Empty);
        }
Exemplo n.º 4
0
        private static DataTable GetPreviewForFixedWidthFile(TxtImportSettings settings, int maxRowsCount)
        {
            var fileName = settings.PathToFile;

            var result = new DataTable();

            int[] columnLengths = null;
            using (var streamReader = new StreamReader(fileName))
            {
                while (streamReader.Peek() != -1 &&
                       (maxRowsCount < 0 ||
                        result.Rows.Count < maxRowsCount))
                {
                    var line = streamReader.ReadLine();
                    if (String.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    if (columnLengths == null)
                    {
                        var columns = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                        if (columns.Length == 0)
                        {
                            continue;
                        }

                        columnLengths = new int[columns.Length];
                        foreach (var column in columns)
                        {
                            result.Columns.Add(column);
                        }
                        for (int i = columns.Length - 1; i >= 0; i--)
                        {
                            var lastIndex    = line.LastIndexOf(columns[i], StringComparison.Ordinal);
                            var columnLength = line.Length - lastIndex;
                            columnLengths[i] = columnLength;
                            line             = line.Remove(lastIndex);
                        }
                    }
                    else
                    {
                        var row = result.NewRow();
                        for (int i = 0; i < columnLengths.Length; i++)
                        {
                            if (line.Length == 0)
                            {
                                break;
                            }
                            var curLen = Math.Min(columnLengths[i], line.Length);
                            var value  = line.Substring(0, curLen).Trim();
                            row[i] = value;
                            line   = line.Substring(curLen);
                        }
                        result.Rows.Add(row);
                    }
                }
            }

            return(result);
        }