-
Notifications
You must be signed in to change notification settings - Fork 295
OLE Objects
EPPlus supports adding OLE Objects by accessing Excelworksheet 's Drawings property. You can add OLE Objects by supplying a file path.
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");
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);
-
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.
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"];
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
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.
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");
- 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.
Sample 5.6 (C#)
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles