Skip to content

Commit bd6c645

Browse files
committed
Added OLE Object Sample
1 parent 7abb4d1 commit bd6c645

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text file for testing
Binary file not shown.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*************************************************************************************************
2+
Required Notice: Copyright (C) EPPlus Software AB.
3+
This software is licensed under PolyForm Noncommercial License 1.0.0
4+
and may only be used for noncommercial purposes
5+
https://polyformproject.org/licenses/noncommercial/1.0.0/
6+
7+
A commercial license to use this software can be purchased at https://epplussoftware.com
8+
*************************************************************************************************
9+
Date Author Change
10+
*************************************************************************************************
11+
01/01/2025 EPPlus Software AB Initial release EPPlus 8
12+
*************************************************************************************************/
13+
using OfficeOpenXml;
14+
using System.Collections.Generic;
15+
using System.IO;
16+
17+
namespace EPPlusSamples._05_Drawings_charts_and_themes._06_OLE_Objects
18+
{
19+
/// <summary>
20+
/// This sample shows how to embed or link files as OLE Objects using EPPLUS.
21+
/// </summary>
22+
public static class OLEObjectsSample
23+
{
24+
public static void RunSample()
25+
{
26+
var myPDF = FileUtil.GetFileInfo("05-Drawings charts and themes\\06-OLE Objects", "MyPDF.pdf");
27+
var myWord = FileUtil.GetFileInfo("05-Drawings charts and themes\\06-OLE Objects", "MyWord.docx");
28+
var myTxt = FileUtil.GetFileInfo("05-Drawings charts and themes\\06-OLE Objects", "MyTextDocument.txt");
29+
var myIcon = FileUtil.GetFileInfo("05-Drawings charts and themes\\06-OLE Objects", "SampleIcon.bmp");
30+
FileInfo newWorkbook = FileUtil.GetCleanFileInfo(@"06-OLE Objects Sample Workbook.xlsx");
31+
32+
33+
/* Embedding a file. */
34+
//Create a workbook and a worksheet.
35+
using var p = new ExcelPackage();
36+
var ws = p.Workbook.Worksheets.Add("Sheet 1");
37+
//Embed the file using AddOleObject method on the drawing.
38+
var EmbeddedWord = ws.Drawings.AddOleObject("MyWord", myWord);
39+
//Save the workbook
40+
p.SaveAs(newWorkbook);
41+
42+
43+
/* Link a file. */
44+
//Create a workbook and a worksheet.
45+
using var p2 = new ExcelPackage(newWorkbook);
46+
var ws2 = p2.Workbook.Worksheets.Add("Sheet 2");
47+
//Link the file using AddOleObject method on the drawing.
48+
var LinkedPDF = ws2.Drawings.AddOleObject("MyPDF", myPDF, true);
49+
//Save the workbook
50+
p2.SaveAs(newWorkbook);
51+
52+
53+
/* Link a file with ExcelOleObjectParameters. */
54+
//Create a workbook and a worksheet.
55+
using var p3 = new ExcelPackage(newWorkbook);
56+
var ws3 = p3.Workbook.Worksheets.Add("Sheet 3");
57+
//Create parameters object.
58+
var parameters = new ExcelOleObjectParameters()
59+
{
60+
LinkToFile = true,
61+
DisplayAsIcon = true,
62+
Extension = ".pdf",
63+
};
64+
//Link the file using AddOleObject method on the drawing.
65+
var LinkedPDF2 = ws3.Drawings.AddOleObject("MyPDF", myPDF, parameters);
66+
//Save the workbook
67+
p3.SaveAs(newWorkbook);
68+
69+
70+
/* Add custom icon. */
71+
//Create a workbook and a worksheet.
72+
using var p4 = new ExcelPackage(newWorkbook);
73+
var ws4 = p4.Workbook.Worksheets.Add("Sheet 4");
74+
//Link the file using AddOleObject method on the drawing.
75+
var txt = ws4.Drawings.AddOleObject("MyText", myTxt, false, true, myIcon);
76+
//Save the workbook
77+
p4.SaveAs(newWorkbook);
78+
79+
80+
/* Copy OLE Object */
81+
//Create a workbook, get worksheet and create new worksheet.
82+
using var p5 = new ExcelPackage(newWorkbook);
83+
var ws1 = p5.Workbook.Worksheets[0];
84+
var ws5 = p5.Workbook.Worksheets.Add("Sheet 5");
85+
//Get OLE Object.
86+
var newPDF = ws1.Drawings[0] as ExcelOleObject;
87+
//Copy OLE Object to a new worksheet.
88+
var copy = ws1.Drawings.Copy(ws5, 1, 4);
89+
//Save the workbook
90+
p5.SaveAs(newWorkbook);
91+
92+
93+
/* Delete OLE Object */
94+
//Create a workbook and get worksheet.
95+
using var p6 = new ExcelPackage(newWorkbook);
96+
ws1 = p6.Workbook.Worksheets[0];
97+
//Get OLE Object.
98+
var myPdfDoc = ws1.Drawings[0] as ExcelOleObject;
99+
//Copy OLE Object to a new worksheet.
100+
ws1.Drawings.Remove(myPdfDoc);
101+
//Save the workbook
102+
p6.SaveAs(newWorkbook);
103+
}
104+
}
105+
}
Binary file not shown.

0 commit comments

Comments
 (0)