Exemplo n.º 1
0
        static void ConvertExcelToJson(string excelFilePath, string exportDir)
        {
            using (var excelFile = new FileStream(excelFilePath, FileMode.Open, FileAccess.Read))
            {
                var excelLoader = new ExcelLoader(excelFile);

                while (excelLoader.HasSheetToRead())
                {
                    var excelSheet = excelLoader.LoadNextSheet();

                    var sheetFileName = $"{GetNameFromPath(excelFile.Name)}.{excelSheet.Name}.json";
                    using (var jsonFile = new FileStream($"{exportDir}/{sheetFileName}", FileMode.Create, FileAccess.Write))
                    {
                        using (var streamWriter = new StreamWriter(jsonFile))
                        {
                            var collectionToJsonConverter = new CollectionToJsonConverter(excelSheet.Properties);

                            streamWriter.Write(collectionToJsonConverter.CreateJSONLiteral(excelSheet.Sheet));
                        }

                        Console.WriteLine($"Exported: {jsonFile.Name}");
                    }
                }
            }
        }