Managing OLE Objects

Introduction

OLE (Object Linking and Embedding) is Microsoft’s framework for a compound document technology. Briefly, a compound document is something like a display desktop that can contain visual and information objects of all kinds: text, calendars, animations, sound, motion video, 3D, continually updated news, controls, and so forth. Each desktop object is an independent program entity that can interact with a user and also communicate with other objects on the desktop.

OLE (Object Linking and Embedding) is supported by many different programs and is used to make content created in one program available in another. For example, you can insert a Microsoft Word document into Microsoft Excel. To see what types of content you can insert, click Object on the Insert menu. Only programs that are installed on the computer and that support OLE objects appear in the Object type box.

Inserting OLE Objects into a Worksheet

Aspose.Cells supports adding, extracting and manipulating OLE objects in worksheets. For this reason, Aspose.Cells has the OleObjectCollection class, used to add a new OLE Object to the collection list. Another class, OleObject, represents an OLE Object. It has some important members:

  • ImageData specifies the image (icon) data of byte array type. The image will be displayed to show the OLE Object in the worksheet.
  • ObjectData specifies the object data in the form of a byte array. This data will be shown in its related program when you double-click on the OLE Object icon.

The following example shows how to add an OLE Object into a worksheet.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(InsertingOLEObjects.class);
// Get the image file.
File file = new File(dataDir + "logo.jpg");
// Get the picture into the streams.
byte[] img = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(img);
// Get the excel file into the streams.
file = new File(dataDir + "Book1.xls");
byte[] data = new byte[(int) file.length()];
fis = new FileInputStream(file);
fis.read(data);
// Instantiate a new Workbook.
Workbook wb = new Workbook();
// Get the first worksheet.
Worksheet sheet = wb.getWorksheets().get(0);
// Add an Ole object into the worksheet with the image shown in MS Excel.
int oleObjIndex = sheet.getOleObjects().add(14, 3, 200, 220, img);
OleObject oleObj = sheet.getOleObjects().get(oleObjIndex);
// Set embedded ole object data.
oleObj.setObjectData(data);
// Save the excel file
wb.save(dataDir + "tstoleobjects.xls");

Extracting OLE Objects in the Workbook

The following example shows how to extract OLE Objects in a Workbook. The example gets different OLE objects from an existing XLS file and saves different files (DOC, XLS, PPT, PDF, etc.) based on the OLE object’s file format type.

Here is the screenshot of the template XLS file, it has different OLE Objects embedded in the first worksheet.

The template file contains four OLE objects

todo:image_alt_text

After running the code, we can save different files based on their respective OLE Objects format types. Following are screenshots for some of the created files.

The extracted XLS file

todo:image_alt_text

The extracted PPT file

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ExtractingOLEObjects.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Get the OleObject Collection in the first worksheet.
OleObjectCollection oles = workbook.getWorksheets().get(0).getOleObjects();
// Loop through all the ole objects and extract each object. in the worksheet.
for (int i = 0; i < oles.getCount(); i++) {
if (oles.get(i).getMsoDrawingType() == MsoDrawingType.OLE_OBJECT) {
OleObject ole = (OleObject) oles.get(i);
// Specify the output filename.
String fileName = dataDir + "tempBook1ole" + i + ".";
// Specify each file format based on the oleformattype.
switch (ole.getFileFormatType()) {
case FileFormatType.DOC:
fileName += "doc";
break;
case FileFormatType.EXCEL_97_TO_2003:
fileName += "Xls";
break;
case FileFormatType.PPT:
fileName += "Ppt";
break;
case FileFormatType.PDF:
fileName += "Pdf";
break;
case FileFormatType.UNKNOWN:
fileName += "Jpg";
break;
default:
fileName += "data";
break;
}
FileOutputStream fos = new FileOutputStream(fileName);
byte[] data = ole.getObjectData();
fos.write(data);
fos.close();
}
}

Extracting Embedded MOL File

Aspose.Cells supports extracting objects of uncommon types like MOL(Molecular data file containing information about atoms and bonds). The following code snippet demonstrates extracting embedded MOL file and saving it to disk by using this sample excel file.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the directories.
String sourceDir = Utils.Get_SourceDirectory();
String outputDir = Utils.Get_OutputDirectory();
Workbook workbook = new Workbook(sourceDir + "EmbeddedMolSample.xlsx");
int index = 1;
for (Object obj : workbook.getWorksheets())
{
Worksheet sheet = (Worksheet)obj;
OleObjectCollection oles = sheet.getOleObjects();
for (Object obj2 : oles)
{
OleObject ole = (OleObject)obj2;
String fileName = outputDir + "OleObject" + index + ".mol ";
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(ole.getObjectData());
fos.flush();
fos.close();
index++;
}
}

Advance topics

  • Access and Modify the Display Label of the Linked Ole Object
  • Automatically refresh OLE object via Microsoft Excel using Aspose.Cells
  • Extract OLE Objects from Workbook
  • Get or Set the Class Identifier of the Embedded OLE Object