示例#1
0
        DataTable ReadDataFromTextFileManually(ExtendedFile file)
        {
            DataTable table = new DataTable(file.Name);

            if (!FileTypes.isTextFile(file))
            {
                return(table);
            }

            var lines     = File.ReadAllLines(file.File.FullName);
            var delimiter = Delimiters.GetDelimiterByHeader(lines.First());
            var fileRows  = SplitLines(lines, delimiter);
            var columns   = fileRows.First()
                            .Where(x => !string.IsNullOrWhiteSpace(x))
                            .Select(x => new DataColumn(x.TrimQuotes())).ToArray();

            table.Columns.AddRange(columns);

            foreach (var fileRow in fileRows.Skip(1))
            {
                var updatedRow = fileRow.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
                if (columns.Length == updatedRow.Length)
                {
                    var newTableRow = table.NewRow();
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        newTableRow[i] = updatedRow[i].TrimQuotes();
                    }
                    table.Rows.Add(newTableRow);
                }
            }
            return(table);
        }