OLE Objects

This module uses the DevExpress Spreadsheet API to generate an Excel document with an embedded OLE object.

OLE (Object Linking and Embedding) objects allow you to embed files or data from other applications (such as Word documents, Visio diagrams, or other media) directly into Excel sheets. This module embeds a predefined Excel document into a newly generated workbook.

Use the Generate a Workbook and Save as... dropdown button to select the output format, generate the document with an embedded object, and download the result.

File Embedded as OLE Object
InvestmentPortfolio.xlsx



using DevExpress.Spreadsheet;

Stream GenerateWorkbookWithOleObject(Stream oleObjectStream, OleObjectType oleObjectType, DocumentFormat outputFormat, Stream oleIconStream = null) {
    using var workbook = new Workbook();
    var worksheet = workbook.Worksheets.First();
    worksheet.Name = "Ole Object";

    string oleObjectRangeReference = "B2:N34";
    SpreadsheetImageSource oleObjectIcon = null;

    // Create SpreadsheetImageSource if an icon stream is specified
    if(oleIconStream != null) {
        oleObjectRangeReference = "B2:C6";
        oleObjectIcon = SpreadsheetImageSource.FromStream(oleIconStream);
    }

    var oleObjectRange = worksheet.Range[oleObjectRangeReference];

    // Create an embedded OLE object
    var oleObjectEmbedded = worksheet.OleObjects.AddEmbeddedOleObject(oleObjectRange, oleObjectStream, oleObjectType, oleObjectIcon);

    // Prepare output stream
    var outputStream = new MemoryStream();

    // Save the document
    workbook.SaveDocument(outputStream, outputFormat);

    outputStream.Position = 0;
    return outputStream;
}