从工作簿中提取OLE对象
Contents
[
Hide
]
有时,你确实需要从工作簿中提取OLE对象。Aspose.Cells支持提取和保存这些Ole对象。
本文介绍了如何在Visual Studio.Net中创建一个控制台应用程序,并用几行简单的代码从工作簿中提取不同的OLE对象。
从工作簿中提取OLE对象
创建模板工作簿
- 在Microsoft Excel中创建一个工作簿。
- 在第一个工作表上添加一个Microsoft Word文档,一个Excel工作簿和一个PDF文档作为OLE对象。
带有OLE对象的模板文档(OleFile.xls) |
---|
![]() |
然后提取OLE对象并将它们保存到硬盘上的相应文件类型。
下载并安装Aspose.Cells
- 下载 Aspose.Cells for .NET。
- 在您的开发计算机上安装它。
所有Aspose组件在安装后都处于评估模式。评估模式没有时间限制,只会在生成的文档中插入水印。
创建一个项目
启动 Visual Studio.Net 并创建一个新的控制台应用程序。该示例将展示一个C#控制台应用程序,但你也可以使用VB.NET。
- 添加引用
- 向项目添加Aspose.Cells组件的引用,例如向…\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll添加引用。
从工作簿中提取OLE对象
下面的代码实际完成了查找和提取OLE对象的工作。OLE对象(DOC、XLS和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-.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 + "oleFile.xlsx"); | |
// 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+ "outOle" + i + "."; | |
// Specify each file format based on the oleobject format type. | |
switch (ole.FileFormatType) | |
{ | |
case FileFormatType.Doc: | |
fileName += "doc"; | |
break; | |
case FileFormatType.Excel97To2003: | |
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(); | |
if (ole.ObjectData != null) | |
{ | |
ms.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
Workbook oleBook = new Workbook(ms); | |
oleBook.Settings.IsHidden = false; | |
oleBook.Save(dataDir + "outOle" + i + ".out.xlsx"); | |
} | |
} | |
// Create the files based on the oleobject format types. | |
else | |
{ | |
if (ole.ObjectData != null) | |
{ | |
FileStream fs = File.Create(fileName); | |
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length); | |
fs.Close(); | |
} | |
} | |
} |