diff --git a/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs b/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs index 0d3cf9ba..765ec1cb 100644 --- a/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs +++ b/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs @@ -1,24 +1,22 @@ using Syncfusion.XlsIO; - +using Syncfusion.Compression.Zip; namespace SplitExcel { class Program { - private static string inputPath = Path.GetFullPath("Data/"); + private static readonly string inputPath = Path.GetFullPath("Data/"); + private static readonly string outputPath = Path.GetFullPath("Output/"); - private static string outputPath = Path.GetFullPath("Output/"); static void Main(string[] args) { string fileName = "Report.xlsx"; - - //Split the Excel document SplitExcelDocument(Path.GetFullPath(inputPath + fileName)); } + /// - /// Split the Excel document from the given path + /// Split the Excel document from the given path and add outputs into a ZIP archive. /// - /// Excel file path private static void SplitExcelDocument(string filePath) { using (ExcelEngine excelEngine = new ExcelEngine()) @@ -30,14 +28,31 @@ private static void SplitExcelDocument(string filePath) workbook.Version = ExcelVersion.Xlsx; - //Loop through each Excel worksheet and save it as a new workbook + // Create a ZIP archive with best compression + ZipArchive zipArchive = new ZipArchive(); + zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best; + foreach (IWorksheet worksheet in worksheets) { IWorkbook newBook = application.Workbooks.Create(0); newBook.Worksheets.AddCopy(worksheet); - newBook.SaveAs(Path.GetFullPath(outputPath + worksheet.Name + ".xlsx")); + string sheetFilePath = Path.GetFullPath(worksheet.Name + ".xlsx"); + + newBook.SaveAs(sheetFilePath); + + // Add that file directly into the ZIP archive + zipArchive.AddFile(sheetFilePath); + + newBook.Close(); } + + // Save the ZIP archive containing all worksheets + string zipFilePath = Path.GetFullPath(outputPath + "Split-Excel-Sheets.zip"); + zipArchive.Save(zipFilePath); + zipArchive.Close(); + + workbook.Close(); } } }