示例#1
0
        private void LoadTextFile()
        {
            if (!OpenFile() || SampleFileName == null || SampleFileName.Trim() == String.Empty)
            {
                return;
            }

            SampleFileText      = String.Empty;
            SampleFormattedData = new DataView();

            if (!System.IO.File.Exists(SampleFileName))
            {
                MessageBox.Show("Could not find specified file", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                SampleFileName = String.Empty;
                return;
            }

            if (CanInfer)
            {
                CanInfer = false;
                InferFromSampleFile();
                LoadSampleText();
                LoadSampleFormatted();
            }
            else
            {
                LoadSampleText();
                LoadSampleFormatted();
            }
        }
示例#2
0
        public void LoadSampleFormatted()
        {
            if (SampleFileName == null || SampleFileName.Trim() == String.Empty || FieldList == null || FieldList.Count == 0)
            {
                return;
            }

            string line       = String.Empty;
            int    counter    = 0;
            var    sampleData = new DataTable();
            string lengthList = "";

            foreach (var field in FieldList)
            {
                lengthList += field.Length + ",";
            }

            if (lengthList != String.Empty)
            {
                lengthList = lengthList.Substring(0, lengthList.Length - 1);
            }

            using (var fileStream = new FileStream(SampleFileName, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                using (var streamReader = new StreamReader(fileStream))
                {
                    bool columnsAdded = false;
                    if (FieldList.Count > 0)
                    {
                        foreach (WrapperAfield header in FieldList)
                        {
                            sampleData.Columns.Add(header.Name);
                        }
                        columnsAdded = true;
                    }

                    FieldParser fieldParser = new FieldParser(TextFileType, Delimiter, TextQualifier, OtherDelimiter, lengthList);
                    int         skipCounter = 0;
                    while ((line = streamReader.ReadLine()) != null && counter < 50)
                    {
                        if (skipCounter < SkipHeaderLines)
                        {
                            skipCounter++;
                            counter++;
                            continue;
                        }

                        string[] values = fieldParser.GetValues(line);
                        if (!columnsAdded && sampleData.Columns.Count == 0)
                        {
                            int columnCounter = 1;
                            Array.ForEach(values, _ => sampleData.Columns.Add("Column" + columnCounter++));
                        }

                        sampleData.Rows.Add(values.Take(sampleData.Columns.Count).ToArray());
                        counter++;
                    }
                }

            if (counter < 50 && sampleData.Rows.Count > SkipFooterLines)
            {
                for (int idx = 0; idx < SkipFooterLines; idx++)
                {
                    sampleData.Rows.RemoveAt(sampleData.Rows.Count - 1);
                }
            }

            SampleFormattedData = sampleData.DefaultView;
        }