Other API Samples

Live SpreadsheetGear API Samples

Workbook Saving Save to Stream or Byte Array

In addition to saving to disk, SpreadsheetGear can also save to a memory stream or byte array using the IWorkbook.SaveToStream(...) and SaveToMemory(...) methods.

// Create a new workbook and some local variables for convenience.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
SpreadsheetGear.IRange cells = worksheet.Cells;

// Rename the worksheet.
worksheet.Name = "My Worksheet";

// Add some cell data.
cells["A1"].Value = "Hello World!";

// Save to stream.
System.IO.Stream stream = workbook.SaveToStream(SpreadsheetGear.FileFormat.OpenXMLWorkbook);

// Or save to byte array.
byte[] bytes = workbook.SaveToMemory(SpreadsheetGear.FileFormat.OpenXMLWorkbook);
Run This Sample
Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.