Beispiel #1
0
 static bool ExcelToSeed(FromOptions opts)
 {
     if (opts.config != null && opts.config.Length > 0)
     {
         opts = BasicOptions.Load(opts.config).FromOptions(
             files: opts.files,
             input: opts.input,
             output: opts.output
             );
     }
     return(SeedTableInterface.ExcelToSeed(opts));
 }
        static DateTime ExcelToSeedCore(IExcelData excelData, string file, FromOptions options, DateTime startTime, DateTime previousTime)
        {
            Log("  sheets");
            var fileName     = Path.GetFileName(file);
            var sheetsConfig = new SheetsConfig(options.only, options.ignore, options.subdivide, options.primary, options.mapping, options.alias);

            foreach (var sheetName in excelData.SheetNames)
            {
                var yamlTableName = sheetsConfig.YamlTableName(fileName, sheetName);
                if (yamlTableName == sheetName)
                {
                    Log($"    {yamlTableName}");
                }
                else
                {
                    Log($"    {yamlTableName} <- {sheetName}");
                }
                if (!sheetsConfig.IsUseSheet(fileName, sheetName, yamlTableName, OnOperation.From))
                {
                    Log("      ignore", "skip");
                    continue;
                }
                var subdivide = sheetsConfig.subdivide(fileName, yamlTableName, OnOperation.From);
                var seedTable = GetSeedTable(excelData, sheetName, options, subdivide);
                if (seedTable.Errors.Count != 0)
                {
                    continue;
                }
                new YamlData(
                    seedTable.ExcelToData(options.requireVersion),
                    subdivide.NeedSubdivide,
                    subdivide.CutPrefix,
                    subdivide.CutPostfix,
                    subdivide.SubdivideFilename,
                    options.format,
                    options.delete,
                    options.yamlColumns
                    ).WriteTo(
                    yamlTableName,
                    options.output,
                    options.seedExtension
                    );
                var now = DateTime.Now;
                DurationLog("      write-time", previousTime, now);
                previousTime = now;
            }
            return(previousTime);
        }
        public static bool ExcelToSeed(FromOptions options)
        {
            Log("engine", options.engine);
            Log("output-directory", options.output);
            var startTime    = DateTime.Now;
            var previousTime = startTime;

            foreach (var file in options.files)
            {
                var filePath = Path.Combine(options.input, file);
                Log(file);
                Log("  full-path", filePath);
                CheckFileExists(filePath);
                switch (options.engine)
                {
                case FromOptions.Engine.OpenXml:
                    using (var excelData = OpenXml.ExcelData.FromFile(filePath, false)) {
                        var parseFinishTime = DateTime.Now;
                        DurationLog("  parse-time", startTime, parseFinishTime);
                        previousTime = ExcelToSeedCore(excelData, file, options, previousTime, parseFinishTime);
                    }
                    break;

                case FromOptions.Engine.ClosedXML:
                    using (var excelData = ClosedXML.ExcelData.FromFile(filePath)) {
                        var parseFinishTime = DateTime.Now;
                        DurationLog("  parse-time", startTime, parseFinishTime);
                        previousTime = ExcelToSeedCore(excelData, file, options, previousTime, parseFinishTime);
                    }
                    break;

                case FromOptions.Engine.EPPlus:
                    using (var excelData = EPPlus.ExcelData.FromFile(filePath)) {
                        var parseFinishTime = DateTime.Now;
                        DurationLog("  parse-time", startTime, parseFinishTime);
                        previousTime = ExcelToSeedCore(excelData, file, options, previousTime, parseFinishTime);
                    }
                    break;
                }
            }
            DurationLog("total", startTime, DateTime.Now);
            return(true);
        }