Example #1
0
        private static ExcelReader CreateReader(ConvertCommandLine cmd)
        {
            switch (cmd.Type)
            {
            case "TRN":
                var trnReader = new TRNExcelReader();
                if (ReadConfiguration(trnReader))
                {
                    return(trnReader);
                }
                return(null);

            case "SDT":
                var sdtReader = new SDTExcelReader();
                if (ReadConfiguration(sdtReader))
                {
                    return(sdtReader);
                }
                return(null);

            case "GRP":
                var grpReader = new GRPExcelReader();
                if (ReadConfiguration(grpReader))
                {
                    return(grpReader);
                }
                return(null);

            default:
                Console.WriteLine("Invalid value for Type argument");
                return(null);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            ConvertCommandLine cmd = new ConvertCommandLine();

            try
            {
                cmd.Parse(args);
                if (cmd.help)
                {
                    Console.WriteLine(cmd.GetUsage());
                    return;
                }
            }
            catch
            {
                Console.WriteLine(cmd.GetUsage());
                return;
            }

            ExcelReader reader = CreateReader(cmd);

            if (reader is null)
            {
                return;
            }

            try
            {
                if (cmd.ExcelFile == null && cmd.Directory == null)
                {
                    Console.WriteLine("Specify the Directory to scan for several xlsx files or the ExcelFile option for an specific file");
                    return;
                }
                string outputXml = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), cmd.OutputFile);
                if (cmd.ExcelFile != null)
                {
                    string inputXlsx = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), cmd.ExcelFile);
                    if (!string.IsNullOrEmpty(inputXlsx))
                    {
                        if (!File.Exists(inputXlsx))
                        {
                            Console.WriteLine($"Input File {inputXlsx} does not exists, please specify an existing xlsx file");
                        }
                        else
                        {
                            reader.ReadExcel(new string[] { inputXlsx }, outputXml, cmd.ContinueOnErrors);
                        }
                    }
                }
                else if (cmd.Directory != null)
                {
                    string inputDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), cmd.Directory);
                    if (!string.IsNullOrEmpty(inputDirectory) && Directory.Exists(inputDirectory))
                    {
                        string[]      files     = Directory.GetFiles(inputDirectory, "*.xlsx");
                        List <string> fullPaths = new List <string>();
                        foreach (string file in files)
                        {
                            fullPaths.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), file));
                        }

                        if (files.Length > 0)
                        {
                            reader.ReadExcel(fullPaths.ToArray(), outputXml, cmd.ContinueOnErrors);
                        }
                        else
                        {
                            Console.WriteLine("Could not find any xlsx on the given directory " + inputDirectory);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Please specify a valid directory to explore for xlsx files");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }