Extract OLE Objects from Workbook
Sometimes, you do need to extract OLE objects from a workbook. Aspose.Cells supports extracting and saving those Ole objects.
This article shows how to create a console application in Visual Studio.Net and extract different OLE objects from a workbook with a few simple lines of code.
Extract OLE Objects from a Workbook
Creating a Template Workbook
- Created a workbook in Microsoft Excel.
- Add a Microsoft Word document, an Excel workbook and a PDF document as OLE objects on first worksheet.
Template document with OLE objects (OleFile.xls) |
---|
![]() |
Next extract the OLE objects and save them to the hard disk with their respective file types.
Download and Install Aspose.Cells
- Download Aspose.Cells for .NET.
- Install it on your development computer.
All Aspose components, when installed, work in evaluation mode. The evaluation mode has no time limit and it only injects watermarks into produced documents.
Create a Project
Start Visual Studio.Net and create a new console application. This example will show a C# console application, but you can use VB.NET too.
- Add References
- Add a reference to Aspose.Cells component to your project, for example add a reference to …\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll
Extract OLE Objects
The code below does the actual work of finding and extracting OLE objects. The OLE objects (DOC, XLS and PDF files) are saved to disk.
// 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 + "oleFile.xlsx"); | |
// 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+ "outOle" + i + "."; | |
// Specify each file format based on the oleobject format type. | |
switch (ole.FileFormatType) | |
{ | |
case FileFormatType.Doc: | |
fileName += "doc"; | |
break; | |
case FileFormatType.Excel97To2003: | |
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(); | |
if (ole.ObjectData != null) | |
{ | |
ms.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
Workbook oleBook = new Workbook(ms); | |
oleBook.Settings.IsHidden = false; | |
oleBook.Save(dataDir + "outOle" + i + ".out.xlsx"); | |
} | |
} | |
// Create the files based on the oleobject format types. | |
else | |
{ | |
if (ole.ObjectData != null) | |
{ | |
FileStream fs = File.Create(fileName); | |
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
fs.Close(); | |
} | |
} | |
} |