从工作簿中提取OLE对象
Contents
[
Hide
]
有时,你确实需要从工作簿中提取 OLE 对象。Aspose.Cells for Python via .NET 支持提取和保存这些 Ole 对象。
本文介绍了如何在Visual Studio.Net中创建一个控制台应用程序,并用几行简单的代码从工作簿中提取不同的OLE对象。
从工作簿中提取OLE对象
创建模板工作簿
- 在Microsoft Excel中创建一个工作簿。
- 在第一个工作表上添加一个Microsoft Word文档,一个Excel工作簿和一个PDF文档作为OLE对象。
带有OLE对象的模板文档(OleFile.xls) |
---|
![]() |
然后提取OLE对象并将它们保存到硬盘上的相应文件类型。
使用 Aspose.Cells for Python Excel 库提取 OLE 对象
下面的代码实际完成了查找和提取OLE对象的工作。OLE对象(DOC、XLS和PDF文件)被保存到磁盘上。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import FileFormatType, Workbook | |
from io import BytesIO | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Open the template file. | |
workbook = Workbook(dataDir + "oleFile.xlsx") | |
# Get the OleObject Collection in the first worksheet. | |
oles = workbook.worksheets[0].ole_objects | |
# Loop through all the oleobjects and extract each object in the worksheet. | |
for i in range(len(oles)): | |
ole = oles[i] | |
# Specify the output filename. | |
fileName = dataDir + "outOle" + str(i) + "." | |
# Specify each file format based on the oleobject format type. | |
if ole.file_format_type == FileFormatType.DOC: | |
fileName = "doc" | |
elif ole.file_format_type == FileFormatType.EXCEL_97_TO_2003: | |
fileName = "Xlsx" | |
elif ole.file_format_type == FileFormatType.PPT: | |
fileName = "Ppt" | |
elif ole.file_format_type == FileFormatType.PDF: | |
fileName = "Pdf" | |
elif ole.file_format_type == FileFormatType.UNKNOWN: | |
fileName = "Jpg" | |
# Save the oleobject as a new excel file if the object type is xls. | |
if ole.file_format_type == FileFormatType.XLSX: | |
ms = BytesIO() | |
if ole.object_data != None: | |
ms.write(ole.object_data) | |
oleBook = Workbook(ms) | |
oleBook.settings.is_hidden = False | |
oleBook.save(dataDir + "outOle" + str(i) + ".out.xlsx") | |
else: | |
if ole.object_data != None: | |
fs = open(fileName, "wb") | |
fs.write(ole.object_data) | |
fs.close() |