管理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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Define a string variable to store the image path. | |
string ImageUrl = dataDir + "logo.jpg"; | |
// Get the picture into the streams. | |
FileStream fs = File.OpenRead(ImageUrl); | |
// Define a byte array. | |
byte[] imageData = new Byte[fs.Length]; | |
// Obtain the picture into the array of bytes from streams. | |
fs.Read(imageData, 0, imageData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Get an excel file path in a variable. | |
string path = dataDir + "book1.xls"; | |
// Get the file into the streams. | |
fs = File.OpenRead(path); | |
// Define an array of bytes. | |
byte[] objectData = new Byte[fs.Length]; | |
// Store the file from streams. | |
fs.Read(objectData, 0, objectData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Add an Ole object into the worksheet with the image | |
// Shown in MS Excel. | |
sheet.OleObjects.Add(14, 3, 200, 220, imageData); | |
// Set embedded ole object data. | |
sheet.OleObjects[0].ObjectData = objectData; | |
// Save the excel file | |
workbook.Save(dataDir + "output.out.xls"); |
从工作簿中提取OLE对象
以下示例显示了如何从工作簿中提取OLE对象。该示例从现有的XLS文件中获取不同的OLE对象,并根据OLE对象的文件格式类型保存不同的文件(DOC、XLS、PPT、PDF等)。
运行代码后,我们可以根据各自OLE对象的格式类型保存不同的文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the template file. | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the OleObject Collection in the first worksheet. | |
Aspose.Cells.Drawing.OleObjectCollection oles = workbook.Worksheets[0].OleObjects; | |
// Loop through all the oleobjects and extract each object. | |
// In the worksheet. | |
for (int i = 0; i < oles.Count; i++) | |
{ | |
Aspose.Cells.Drawing.OleObject ole = oles[i]; | |
// Specify the output filename. | |
string fileName = dataDir + "ole_" + i + "."; | |
// Specify each file format based on the oleobject format type. | |
switch (ole.FileFormatType) | |
{ | |
case FileFormatType.Doc: | |
fileName += "doc"; | |
break; | |
case FileFormatType.Xlsx: | |
fileName += "Xlsx"; | |
break; | |
case FileFormatType.Ppt: | |
fileName += "Ppt"; | |
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 (ole.FileFormatType == FileFormatType.Xlsx) | |
{ | |
MemoryStream ms = new MemoryStream(); | |
ms.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
Workbook oleBook = new Workbook(ms); | |
oleBook.Settings.IsHidden = false; | |
oleBook.Save(dataDir + "Excel_File" + i + ".out.xlsx"); | |
} | |
// Create the files based on the oleobject format types. | |
else | |
{ | |
FileStream fs = File.Create(fileName); | |
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
fs.Close(); | |
} | |
} |
提取嵌入的MOL文件
Aspose.Cells支持提取MOL(包含有关原子和键信息的分子数据文件)等不常见类型的对象。以下代码片段演示了提取内嵌的MOL文件并将其保存到磁盘中的操作,使用了这个示例Excel文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//directories | |
string SourceDir = RunExamples.Get_SourceDirectory(); | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
Workbook workbook = new Workbook(SourceDir + "EmbeddedMolSample.xlsx"); | |
var index = 1; | |
foreach (Worksheet sheet in workbook.Worksheets) | |
{ | |
OleObjectCollection oles = sheet.OleObjects; | |
foreach (var ole in oles) | |
{ | |
string fileName = outputDir + "OleObject" + index + ".mol "; | |
FileStream fs = File.Create(fileName); | |
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
fs.Close(); | |
index++; | |
} | |
} |