|
| 1 | +--- |
| 2 | +title: Extract embedded OLE files from Excel as streams | Syncfusion |
| 3 | +description: This page shows how to extract embedded OLE objects from Excel as streams using the Syncfusion .NET Excel (XlsIO) library. |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to extract embedded OLE files from an Excel workbook as streams? |
| 10 | + |
| 11 | +You can extract OLE objects in an Excel workbook as streams using XlsIO. The following example demonstrates how to retrieve embedded files from a worksheet. |
| 12 | +{% tabs %} |
| 13 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 14 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 15 | +{ |
| 16 | + //Create worksheet |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 20 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 21 | + |
| 22 | + FileStream embedStream = new FileStream("../../../Sample.docx", FileMode.Open); |
| 23 | + FileStream imageStream = new FileStream("../../../wordIcon.jpg", FileMode.Open); |
| 24 | + |
| 25 | + //Create image stream |
| 26 | + Image image = Image.FromStream(imageStream); |
| 27 | + |
| 28 | + //Add ole object |
| 29 | + IOleObject oleObject = worksheet.OleObjects.Add(embedStream, image, OleObjectType.WordDocument); |
| 30 | + |
| 31 | + // Get the OLE part stream. |
| 32 | + Image image1 = Image.FromStream(worksheet.OleObjects[0].GetEmbeddedOleStream()); |
| 33 | + MemoryStream memory = new MemoryStream(image1.ImageData); |
| 34 | + |
| 35 | + //Saving the workbook as stream |
| 36 | + FileStream stream = new FileStream("ExtractedFile.xlsx", FileMode.Create, FileAccess.Write); |
| 37 | + memory.CopyTo(stream); |
| 38 | + workbook.SaveAs(stream); |
| 39 | + stream.Dispose(); |
| 40 | +} |
| 41 | +{% endhighlight %} |
| 42 | + |
| 43 | +{% endtabs %} |
0 commit comments