0

プログラム コードは、Excel ファイルで無効な最初の列をチェックするとすぐに、残りの列をチェックしません。ループを続行して残りの列をチェックする方法はありますか?

ValidationFile コードを以下に示します。Excel の列名を調べて、列のサイズが 128 文字以下で空でないか、全体が空でないかをチェックします。

 public Boolean InvalidColumnNames()
        {
            using (ReadAndStreamFile())
            {
                reader.Read();
                {
                    int counter = 0;
                    var ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToList();
                    if (ColumnsNames.Count != 0 && reader.Read() == true)
                    {
                        for (int columnNumber = 0; columnNumber < ColumnsNames.Count; columnNumber++)
                        {
                            var excel = new ExcelDataReaderFile();
                            var cellAddress = excel.GetAddress(counter, 0);
                            counter += 1;
                            if (ColumnsNames[columnNumber] != null && ColumnsNames[columnNumber].ToString().Length > columnNameSizeLimit)
                            {
                               Console.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is {columnNumber.ToString().Length} characters long and exceeds {columnNameSizeLimit} character column name limit. Supply a valid column name.");
                                return true;
                            }
                            else if (ColumnsNames[columnNumber] == null)
                            {
                                Conseol.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is empty. Supply a valid column name.");
                                return true;
                            }
                            else
                            {
                               // return true;
                            }
                            continue;
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[{GetFileName(file)}]{reader.Name} is empty and cannot be validated. Supply a non-empty file.");
                        return true;
                    };
                }
                reader.Dispose();
                reader.Close();
                return false;
            }
        }

以下は、Invalid column names が true であるかどうかを説明する Program Main メソッドであり、そのようなファイルの検証に失敗し、それ以外の場合は検証に合格したことを示しています。

 class program
        {
            public static void Main(string[] args)
            {
                string path = ConfigurationManager.AppSettings["path"].ToString();
                string search = ConfigurationManager.AppSettings["search"].ToString();


                ExcelDataReaderFile ExcelDataReaderFile = new ExcelDataReaderFile();
                string[] fileEntries = Directory.GetFiles(path, "*" + search + "*", SearchOption.AllDirectories);

                dynamic counter = 0;
                BasicValidation BasicValidation = new BasicValidation();
                    foreach (string file in fileEntries)
                    {
                        BasicValidation basicValidation = new BasicValidation(file, 128, 32767, 128, 128);
                        counter += 1;

                        Logger.Info($"Validating {basicValidation.GetFileName(file)}");

                        //Console.ForegroundColor = ConsoleColor.Blue;
                        if (basicValidation.InvalidColumnNames())
                      {
                            //if the above statments are true then run those methods and log an error
                            //Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {Regex.Unescape(path)} has failed validation. Check the error log for details.");
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();
                        }
                        else
                        {
                            Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {file} has passed validation checks.");
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();
                        }
                }
                else
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
     }
4

1 に答える 1