11using Syncfusion . XlsIO ;
2-
2+ using Syncfusion . Compression . Zip ;
33
44namespace SplitExcel
55{
66 class Program
77 {
8- private static string inputPath = Path . GetFullPath ( "Data/" ) ;
8+ private static readonly string inputPath = Path . GetFullPath ( "Data/" ) ;
9+ private static readonly string outputPath = Path . GetFullPath ( "Output/" ) ;
910
10- private static string outputPath = Path . GetFullPath ( "Output/" ) ;
1111 static void Main ( string [ ] args )
1212 {
1313 string fileName = "Report.xlsx" ;
14-
15- //Split the Excel document
1614 SplitExcelDocument ( Path . GetFullPath ( inputPath + fileName ) ) ;
1715 }
16+
1817 /// <summary>
19- /// Split the Excel document from the given path
18+ /// Split the Excel document from the given path and add outputs into a ZIP archive.
2019 /// </summary>
21- /// <param name="filePath">Excel file path</param>
2220 private static void SplitExcelDocument ( string filePath )
2321 {
2422 using ( ExcelEngine excelEngine = new ExcelEngine ( ) )
@@ -30,14 +28,31 @@ private static void SplitExcelDocument(string filePath)
3028
3129 workbook . Version = ExcelVersion . Xlsx ;
3230
33- //Loop through each Excel worksheet and save it as a new workbook
31+ // Create a ZIP archive with best compression
32+ ZipArchive zipArchive = new ZipArchive ( ) ;
33+ zipArchive . DefaultCompressionLevel = Syncfusion . Compression . CompressionLevel . Best ;
34+
3435 foreach ( IWorksheet worksheet in worksheets )
3536 {
3637 IWorkbook newBook = application . Workbooks . Create ( 0 ) ;
3738 newBook . Worksheets . AddCopy ( worksheet ) ;
3839
39- newBook . SaveAs ( Path . GetFullPath ( outputPath + worksheet . Name + ".xlsx" ) ) ;
40+ string sheetFilePath = Path . GetFullPath ( worksheet . Name + ".xlsx" ) ;
41+
42+ newBook . SaveAs ( sheetFilePath ) ;
43+
44+ // Add that file directly into the ZIP archive
45+ zipArchive . AddFile ( sheetFilePath ) ;
46+
47+ newBook . Close ( ) ;
4048 }
49+
50+ // Save the ZIP archive containing all worksheets
51+ string zipFilePath = Path . GetFullPath ( outputPath + "Split-Excel-Sheets.zip" ) ;
52+ zipArchive . Save ( zipFilePath ) ;
53+ zipArchive . Close ( ) ;
54+
55+ workbook . Close ( ) ;
4156 }
4257 }
4358 }
0 commit comments