コード例 #1
0
        public static int[] GetColumnIndexes(string[] columnNames, string[] requiredColumns)
        {
            int[] requiredColumnIndexes = new int[requiredColumns.Length];
            for (int i = 0; i < requiredColumns.Length; i++)
            {
                requiredColumnIndexes[i] = CsvInfo.GetColumnIndex(columnNames, requiredColumns[i]);
            }

            return(requiredColumnIndexes);
        }
コード例 #2
0
        public string[] Read(string filePath, string[] requiredColumns)
        {
            #region Variables
            List <string> fileData              = new List <string>();
            string[]      columnNames           = CsvInfo.GetColumns(filePath);
            int[]         requiredColumnIndexes = CsvInfo.GetColumnIndexes(columnNames, requiredColumns);
            #endregion

            using (StreamReader csvReader = File.OpenText(filePath))
            {
                var line = "";
                while ((line = csvReader.ReadLine()) != null)
                {
                    ProcessLine(fileData, requiredColumnIndexes, line);
                }
            }

            return(fileData.ToArray());
        }
コード例 #3
0
        public static bool CheckFormat(string path, IEnumerable <string> columnNames)
        {
            var columnsInFile = CsvInfo.GetColumns(path);

            if (columnsInFile == null)
            {
                return(false);
            }
            var noOfColumns = columnsInFile.Length;

            foreach (var column in columnNames)
            {
                if (!Array.Exists(columnsInFile, element => element.Contains(column, StringComparison.OrdinalIgnoreCase)))
                {
                    return(false);
                }
            }
            ICsvFileReader reader = new CsvFileReader();
            var            lines  = reader.Read(path);

            return(IsNoOfColumnsSameInEachRow(lines, noOfColumns));
        }