管理 OLE 对象
介绍
OLE(Object Linking and Embedding)是Microsoft的一种复合文档技术框架。简而言之,复合文档类似于显示桌面,可以包含各种视觉和信息对象:文本、日历、动画、声音、动态视频、3D、不断更新的新闻、控件等。每个桌面对象都是一个独立的程序实体,可以与用户交互,也可以与桌面上的其他对象进行通信。
许多不同的程序都支持 OLE(对象链接和嵌入),用于使在一个程序中创建的内容在另一个程序中可用。例如,您可以将 Microsoft 的 Word 文档插入到 Microsoft 的 Excel 中。要查看您可以插入的内容类型,请单击目的在插入菜单。只有安装在计算机上并支持 OLE 对象的程序才会出现在对象类型盒子。
将 OLE 对象插入工作表
Aspose.Cells 支持在工作表中添加、提取和操作 OLE 对象。因此,Aspose.Cells 具有OleObject集合类,用于将新的 OLE 对象添加到集合列表中。另一个班级,对象代表一个 OLE 对象。它有一些重要的成员:
以下示例显示如何将 OLE 对象添加到工作表中。
// 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(InsertingOLEObjects.class); | |
// Get the image file. | |
File file = new File(dataDir + "logo.jpg"); | |
// Get the picture into the streams. | |
byte[] img = new byte[(int) file.length()]; | |
FileInputStream fis = new FileInputStream(file); | |
fis.read(img); | |
// Get the excel file into the streams. | |
file = new File(dataDir + "Book1.xls"); | |
byte[] data = new byte[(int) file.length()]; | |
fis = new FileInputStream(file); | |
fis.read(data); | |
// Instantiate a new Workbook. | |
Workbook wb = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Add an Ole object into the worksheet with the image shown in MS Excel. | |
int oleObjIndex = sheet.getOleObjects().add(14, 3, 200, 220, img); | |
OleObject oleObj = sheet.getOleObjects().get(oleObjIndex); | |
// Set embedded ole object data. | |
oleObj.setObjectData(data); | |
// Save the excel file | |
wb.save(dataDir + "tstoleobjects.xls"); |
提取工作簿中的 OLE 对象
以下示例显示如何在工作簿中提取 OLE 对象。该示例从现有的 XLS 文件中获取不同的 OLE 对象,并根据 OLE 对象的文件格式类型保存不同的文件(DOC、XLS、PPT、PDF 等)。
这是模板 XLS 文件的屏幕截图,它在第一个工作表中嵌入了不同的 OLE 对象。
模板文件包含四个 OLE 对象
运行代码后,我们可以根据各自的 OLE Objects 格式类型保存不同的文件。以下是一些已创建文件的屏幕截图。
提取的 XLS 文件
提取的PPT文件
// 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(ExtractingOLEObjects.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the OleObject Collection in the first worksheet. | |
OleObjectCollection oles = workbook.getWorksheets().get(0).getOleObjects(); | |
// Loop through all the ole objects and extract each object. in the worksheet. | |
for (int i = 0; i < oles.getCount(); i++) { | |
if (oles.get(i).getMsoDrawingType() == MsoDrawingType.OLE_OBJECT) { | |
OleObject ole = (OleObject) oles.get(i); | |
// Specify the output filename. | |
String fileName = dataDir + "tempBook1ole" + i + "."; | |
// Specify each file format based on the oleformattype. | |
switch (ole.getFileFormatType()) { | |
case FileFormatType.DOC: | |
fileName += "doc"; | |
break; | |
case FileFormatType.EXCEL_97_TO_2003: | |
fileName += "Xls"; | |
break; | |
case FileFormatType.PPT: | |
fileName += "Ppt"; | |
break; | |
case FileFormatType.PDF: | |
fileName += "Pdf"; | |
break; | |
case FileFormatType.UNKNOWN: | |
fileName += "Jpg"; | |
break; | |
default: | |
fileName += "data"; | |
break; | |
} | |
FileOutputStream fos = new FileOutputStream(fileName); | |
byte[] data = ole.getObjectData(); | |
fos.write(data); | |
fos.close(); | |
} | |
} |
提取嵌入式 MOL 文件
Aspose.Cells 支持提取不常见类型的对象,如 MOL(包含有关原子和键的信息的分子数据文件)。下面的代码片段演示了如何提取嵌入的 MOL 文件并将其保存到磁盘示例 excel 文件.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the directories. | |
String sourceDir = Utils.Get_SourceDirectory(); | |
String outputDir = Utils.Get_OutputDirectory(); | |
Workbook workbook = new Workbook(sourceDir + "EmbeddedMolSample.xlsx"); | |
int index = 1; | |
for (Object obj : workbook.getWorksheets()) | |
{ | |
Worksheet sheet = (Worksheet)obj; | |
OleObjectCollection oles = sheet.getOleObjects(); | |
for (Object obj2 : oles) | |
{ | |
OleObject ole = (OleObject)obj2; | |
String fileName = outputDir + "OleObject" + index + ".mol "; | |
FileOutputStream fos = new FileOutputStream(fileName); | |
fos.write(ole.getObjectData()); | |
fos.flush(); | |
fos.close(); | |
index++; | |
} | |
} |