Skip to content

OLE Objects

AdrianEPPlus edited this page Dec 17, 2024 · 13 revisions

EPPlus supports adding OLE Objects by accessing Excelworksheet 's Drawings property. You can add OLE Objects by supplying a file path.

Example 1 - Adding an Embedded OLE Object

To add an embedded OLE object you use the AddOleObject method on the worksheets Drawings property. You need to supply a filepath as a string.

using ExcelPackage package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Sheet 1");
var oleObject = worksheet.Drawings.AddOleObject("MyOleObject", @"C:\MyFiles\MyPDF.pdf");

Example 2 - Add a Linked OLE Object

Instead of embedding an object into the worksheet, EPPlus also supports linking to an object. Use AddOleObject method on the worksheets Drawing property. You need to supply a filepath as a string and true as the LinkToFile argument.

using ExcelPackage package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Sheet 1");
var oleObject = worksheet.Drawings.AddOleObject("MyLinkedObject", @"C:\MyFiles\MyPDF.pdf", true);

ExcelOleObjectParameters

  • bool LinkToFile - Optional: If true the file will be linked.
  • bool DisplayAsIcon - Optional: If true the drawings will be displayed as an icon.
  • string ProgId - Set custom ProgId for the OLE Object
  • string Extension - The file extension.

Read existing OLE Objects

You can access exsisting OLE Objects by using the Drawings property on the worksheet. You can use the name or the index to get the ExcelOleObject.

using ExcelPackage package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets[0];
var MyTextFile = worksheet.Drawings["MyTextFile"];

Copy OLE Objects

As with any other Drawing you can make a copy of the OLE Object. Use the Copy method on the drawing and supply the target worksheet and position. Offsets in row and column are optional parameters.

var MyTextFile = worksheet.Drawings["MyTextFile"];
MyTextFile.Copy(AnotherWorksheet, 5, 1);

Copy arguments

Copy accepts the following arguments:

  • ExcelWorksheet worksheet - The target worksheet to copy the object to.
  • int row - The row position for the object copy.
  • int col - The column position for the object copy.
  • int rowOffset - Optional: The row offset.
  • int colOffset - Optional: The column offset.

Remove an OLE Object

You can remove an OLE from a worksheet via the Drawings.Remove method. You can supply the OLE Object, name or its index in the Drawings collection.

//Using the object to remove
var MyTextFile = worksheet.Drawings["MyTextFile"];
worksheet.Drawings.Remove(MyTextFile);

//Using the index to remove
worksheet.Drawings.Remove(0);

//Using the name to remove
worksheet.Drawings.Remove("MyTextFile");

Notes

  • When opening a workbook with an OLE object added with EPPlus in Excel and the DisplayAsIcon property is set to false. The Ole Object will be displayed as an icon until you open the OLE object inside Excel. This is due to limitations in EPPlus how we create the image representing the OLE Object.
  • EPPlus has a max file size of 2GB for embedded OLE objects.

See also

Sample 5.6 (C#)

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally