Loading and saving workbook on Mac OS

26 Apr 20241 minute to read

Opening an existing workbook from stream

You can open an existing workbook from stream by using the overloads of Open methods of IWorkbooks interface.

//Creates a new instance for ExcelEngine
ExcelEngine excelEngine = new ExcelEngine();

//Initialize IApplication
IApplication application = excelEngine.Excel;

//Load the file into stream
Assembly executingAssembly = typeof(Program).GetTypeInfo().Assembly;
Stream inputStream = executingAssembly.GetManifestResourceStream("XlsIOSample.Sample.xlsx");

//Loads or open an existing workbook through Open method of IWorkbooks
IWorkbook workbook = application.Workbooks.Open(inputStream);

Saving an Excel workbook to stream

You can also save the created or manipulated workbook to stream using overloads of SaveAs methods.

//Creates a new instance for ExcelEngine
ExcelEngine excelEngine = new ExcelEngine();

//Initialize IApplication
IApplication application = excelEngine.Excel;

//Load the file into stream
FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);

//Loads or open an existing workbook through Open method of IWorkbooks
IWorkbook workbook = application.Workbooks.Open(inputStream);

//To-Do some manipulation
//To-Do some manipulation

//Set the version of the workbook
workbook.Version = ExcelVersion.Xlsx;

//Initialize stream
FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create);

//Save the workbook as stream
workbook.SaveAs(outputStream);