Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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));
}

/// <summary>
/// Split the Excel document from the given path
/// Split the Excel document from the given path and add outputs into a ZIP archive.
/// </summary>
/// <param name="filePath">Excel file path</param>
private static void SplitExcelDocument(string filePath)
{
using (ExcelEngine excelEngine = new ExcelEngine())
Expand All @@ -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();
}
}
}
Expand Down
Loading