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 the 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:
- The ImageData property specifies the image (icon) data of byte array type. The image will be displayed to show the OLE Object in the worksheet.
- The ObjectData property 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(s) into a worksheet.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Define a string variable to store the image path. | |
string ImageUrl = dataDir + "logo.jpg"; | |
// Get the picture into the streams. | |
FileStream fs = File.OpenRead(ImageUrl); | |
// Define a byte array. | |
byte[] imageData = new Byte[fs.Length]; | |
// Obtain the picture into the array of bytes from streams. | |
fs.Read(imageData, 0, imageData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Get an excel file path in a variable. | |
string path = dataDir + "book1.xls"; | |
// Get the file into the streams. | |
fs = File.OpenRead(path); | |
// Define an array of bytes. | |
byte[] objectData = new Byte[fs.Length]; | |
// Store the file from streams. | |
fs.Read(objectData, 0, objectData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Add an Ole object into the worksheet with the image | |
// Shown in MS Excel. | |
sheet.OleObjects.Add(14, 3, 200, 220, imageData); | |
// Set embedded ole object data. | |
sheet.OleObjects[0].ObjectData = objectData; | |
// Save the excel file | |
workbook.Save(dataDir + "output.out.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.
After running the code, we can save different files based on their respective OLE Objects format types.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the template file. | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the OleObject Collection in the first worksheet. | |
Aspose.Cells.Drawing.OleObjectCollection oles = workbook.Worksheets[0].OleObjects; | |
// Loop through all the oleobjects and extract each object. | |
// In the worksheet. | |
for (int i = 0; i < oles.Count; i++) | |
{ | |
Aspose.Cells.Drawing.OleObject ole = oles[i]; | |
// Specify the output filename. | |
string fileName = dataDir + "ole_" + i + "."; | |
// Specify each file format based on the oleobject format type. | |
switch (ole.FileFormatType) | |
{ | |
case FileFormatType.Doc: | |
fileName += "doc"; | |
break; | |
case FileFormatType.Xlsx: | |
fileName += "Xlsx"; | |
break; | |
case FileFormatType.Ppt: | |
fileName += "Ppt"; | |
break; | |
case FileFormatType.Pdf: | |
fileName += "Pdf"; | |
break; | |
case FileFormatType.Unknown: | |
fileName += "Jpg"; | |
break; | |
default: | |
//........ | |
break; | |
} | |
// Save the oleobject as a new excel file if the object type is xls. | |
if (ole.FileFormatType == FileFormatType.Xlsx) | |
{ | |
MemoryStream ms = new MemoryStream(); | |
ms.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
Workbook oleBook = new Workbook(ms); | |
oleBook.Settings.IsHidden = false; | |
oleBook.Save(dataDir + "Excel_File" + i + ".out.xlsx"); | |
} | |
// Create the files based on the oleobject format types. | |
else | |
{ | |
FileStream fs = File.Create(fileName); | |
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
fs.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-.NET | |
//directories | |
string SourceDir = RunExamples.Get_SourceDirectory(); | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
Workbook workbook = new Workbook(SourceDir + "EmbeddedMolSample.xlsx"); | |
var index = 1; | |
foreach (Worksheet sheet in workbook.Worksheets) | |
{ | |
OleObjectCollection oles = sheet.OleObjects; | |
foreach (var ole in oles) | |
{ | |
string fileName = outputDir + "OleObject" + index + ".mol "; | |
FileStream fs = File.Create(fileName); | |
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
fs.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
- Inserting a WAV file as an Ole Object