从工作簿中提取OLE对象
Contents
[
Hide
]
有时,您确实需要从工作簿中提取 OLE 对象。Aspose.Cells 支持提取和保存这些 OLE 对象,正如本文所示。
从工作簿中提取OLE对象
创建模板工作簿
- 在 Microsoft Excel 中创建电子表格。
- 在第一个工作表上添加一个 Microsoft Word 文档、一个 Excel 工作簿和一个 PDF 文档作为 OLE 对象。
从工作簿中提取OLE对象
下面的代码实际上是查找和提取 DOCX、XLSX、PPTX 和 PDF 文件的实际工作。
This file contains 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ExtractOLEObjects.class); | |
// Instantiating a Workbook object, Open the template file. | |
Workbook workbook = new Workbook(dataDir + "oleFile.xlsx"); | |
// Get the OleObject Collection in the first worksheet. | |
OleObjectCollection objects = workbook.getWorksheets().get(0).getOleObjects(); | |
// Loop through all the OleObjects and extract each object in the worksheet. | |
for (int i = 0; i < objects.getCount(); i++) { | |
OleObject object = objects.get(i); | |
// Specify the output filename. | |
String fileName = "D:/object" + i + "."; | |
// Specify each file format based on the OleObject format type. | |
switch (object.getFileFormatType()) { | |
case FileFormatType.DOCX: | |
fileName += "docx"; | |
break; | |
case FileFormatType.XLSX: | |
fileName += "xlsx"; | |
break; | |
case FileFormatType.PPTX: | |
fileName += "pptx"; | |
break; | |
case FileFormatType.PDF: | |
fileName += "pdf"; | |
break; | |
case FileFormatType.UNKNOWN: | |
fileName += "jpg"; | |
break; | |
default: | |
// ........ | |
break; | |
} | |
// Save the OleObject as a new excel file if the object type is xls. | |
if (object.getFileFormatType() == FileFormatType.XLSX) { | |
byte[] bytes = object.getObjectData(); | |
InputStream is = new ByteArrayInputStream(bytes); | |
Workbook oleBook = new Workbook(is); | |
oleBook.getSettings().setHidden(false); | |
oleBook.save(fileName); | |
} | |
// Create the files based on the OleObject format types. | |
else { | |
FileOutputStream fos = new FileOutputStream(fileName); | |
fos.write(object.getObjectData()); | |
fos.close(); | |
} | |
} |
结论:
本文演示了如何使用 Aspose.Cells 从工作簿中提取 OLE 对象。希望它能为您提供一些见解,让您能够在自己的场景中使用这些选项。