Skip to main content
All docs
V25.1
  • OLE Objects in Spreadsheet Control for WPF

    • 4 minutes to read

    OLE (Object Linking and Embedding) technology allows you to insert data (spreadsheets, images, presentations, charts, equations, and much more) from external applications into your document. Use the Spreadsheet for WPF to load, add, extract, or remove OLE objects. You can print documents with OLE objects and export them to PDF.

    spreadsheet for wpf ole objects

    Note

    The Spreadsheet for WPF supports OLE objects in OpenXML-based formats only (XLSX, XLSM, XLTX, and XLTM).

    Obtain OLE Objects in a Worksheet

    The Worksheet.OleObjects property returns an OleObjectCollection that stores all OLE objects within the worksheet. Each individual OLE object within the collection is an OleObject interface.

    using DevExpress.Spreadsheet;
    
        Worksheet worksheet = spreadsheetControl1.Document.Worksheets[0];
    
        // Get the collection of OLE objects:
        OleObjectCollection oleObjects = worksheet.OleObjects;
    

    You use the following OleObject members to obtain information about the corresponding OLE object:

    OleObject.InsertType
    Returns how the OLE object is stored within the worksheet. Possible options: Linked, Embedded.
    OleObject.AsEmbeddedContent

    Allows you to access the content of an embedded OLE object. You can call the OleObjectEmbeddedContent.GetRawData method to obtain raw binary content or call the OleObjectEmbeddedContent.SaveAs method to save the embedded content to a file or stream.

    var embeddedContent = worksheet.OleObjects[0].AsEmbeddedContent();
    var contentData = embeddedContent.GetRawData();
    
    OleObject.AsLinkedContent

    Allows you to access the content of a linked OLE object. For linked OLE objects, you can use the OleObjectLinkedContent.FileName property to obtain the corresponding file name.

    var linkedContent = worksheet.OleObjects[0].AsLinkedContent();
    var path = linkedContent.FileName;
    
    OleObject.OleObjectTypeId

    Returns an ID that identifies the content type (for example, “ExcelWorksheet” or “AdobeAcrobatDocument”). See the OleObjectType structure for all possible options.

    var typeID = worksheet.OleObjects[0].OleObjectTypeId;
    
    OleObject.RepresentationType
    Returns how the OLE object is displayed in the worksheet. Possible options: Content, Icon.

    You can also call OleObjectCollection.GetOleObjectById and OleObjectCollection.GetOleObjectsByName methods to obtain a specific OLE object by its ID or name.

    OLE objects can also be accessed in the Worksheet.Shapes collection:

    foreach (var obj in worksheet.Shapes) {
        if (obj.ShapeType is DevExpress.Spreadsheet.ShapeType.OleObject) {
            // Do something
        }
    }
    

    Add a Linked OLE Object

    A worksheet stores linked objects as links to corresponding files.

    Call the OleObjectCollection.AddLinkedOleObject method to add a linked OLE object to a worksheet:

    Worksheet worksheet = spreadsheetControl1.Document.Worksheets[0];
    CellRange oleIconRange = worksheet.Range["B4:D6"];
    SpreadsheetImageSource oleIcon = SpreadsheetImageSource.FromFile("oleIcon.png");
    
    // Create a linked OLE object
    OleObject oleObjectLinked = worksheet.OleObjects.AddLinkedOleObject(
        oleIconRange, "package.pdf", OleObjectType.Package, oleIcon);
    

    Add an Embedded OLE Object

    A worksheet stores embedded objects incorporated directly to the spreadsheet document in binary format.

    Call the OleObjectCollection.AddEmbeddedOleObject method to add an embedded OLE object to a worksheet:

    Worksheet worksheet = spreadsheetControl1.Document.Worksheets[0];
    CellRange oleIconRange = worksheet.Range["B4:D6"];
    SpreadsheetImageSource oleIcon = SpreadsheetImageSource.FromFile("oleIcon.png");
    
    // Create an embedded OLE object from byte array
    byte[] sourceData = File.ReadAllBytes("package.pdf");
    OleObject oleObjectEmbedded1 = worksheet.OleObjects.AddEmbeddedOleObject(
        oleIconRange, sourceData, OleObjectType.Package, oleIcon);
    
    // Create an embedded OLE object from file stream
    using (var stream = File.OpenRead("package.pdf")) {
        OleObject oleObjectEmbedded2 = worksheet.OleObjects.AddEmbeddedOleObject(
        oleIconRange, stream, OleObjectType.Package, oleIcon);
    }
    

    Remove an OLE Object from a Worksheet

    Call the following methods to remove an OLE object from the document:

    OleObject oleObject = worksheet.OleObjects[0];
    
    // Remove the current OLE object
    oleObject.Delete();
    // or
    worksheet.OleObjects.Remove(oleObject);
    // or
    worksheet.OleObjects.RemoveAt(0);
    
    // Remove all OLE objects in the collection
    worksheet.OleObjects.Clear();
    

    Limitations

    • The SpreadsheetControl does not contain User Interface elements to manage OLE objects.
    • The SpreadsheetControl displays OLE objects as images. You cannot modify embedded data or open files associated with OLE objects.