ブックから 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(); | |
} | |
} | |
} |