管理OLE对象
介绍
OLE (对象链接和嵌入) 是微软的复合文档技术框架。简而言之,复合文档类似于一个显示桌面,可以包含各种视觉和信息对象: 文本、日历、动画、声音、动态视频、3D、不断更新的新闻、控件等。每个桌面对象都是一个独立的程序实体,可以与用户交互,并与桌面上的其他对象进行通信。
OLE (对象链接和嵌入) 受到许多不同程序的支持,并用于使在一个程序中创建的内容在另一个程序中可用。例如,您可以将Microsoft Word文档插入Microsoft Excel。要查看可以插入的内容的类型,请单击插入菜单上的对象。只有安装在计算机上并支持OLE对象的程序才会出现在对象类型框中。
向工作表中插入OLE对象
Aspose.Cells支持在工作表中添加、提取和操纵OLE对象。因此,Aspose.Cells有OleObjectCollection类,用于将新的OLE对象添加到集合列表中。另一个类,OleObject,表示一个OLE对象。它有一些重要的成员:
- ImageData 指定字节数组类型的图像(图标)数据。图像将被显示,以显示工作表中的OLE对象。
- ObjectData 指定以字节数组的形式的对象数据。当您双击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对象格式类型保存不同的文件。以下是一些创建文件的屏幕截图。
提取的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文件并将其保存到磁盘中,使用的是这个sample excel file.
// 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++; | |
} | |
} |