ブックから OLE オブジェクトを抽出する

ブックから OLE オブジェクトを抽出する

テンプレート ワークブックの作成

  1. Microsoft Excel でワークブックを作成しました。
  2. 最初のワークシートに Microsoft Word ドキュメント、Excel ワークブック、および PDF ドキュメントを OLE オブジェクトとして追加します。
OLE オブジェクトを含むテンプレート ドキュメント (OleFile.xls)
todo:画像_代替_文章

次に、OLE オブジェクトを抽出し、それぞれのファイル タイプでハード ディスクに保存します。

Aspose.Cells をダウンロードしてインストールします

  1. ダウンロード Aspose.Cells for .NET.
  2. 開発用コンピューターにインストールします。

Aspose コンポーネントはすべて、インストールすると評価モードで動作します。評価モードには時間制限がなく、生成されたドキュメントに透かしを挿入するだけです。

プロジェクトを作成する

始めるVisual Studio.Net新しいコンソール アプリケーションを作成します。この例では C# コンソール アプリケーションを示していますが、VB.NET も使用できます。

  1. 参照を追加
  2. Aspose.Cells コンポーネントへの参照をプロジェクトに追加します。たとえば、…\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll への参照を追加します。

OLE オブジェクトを抽出する

以下のコードは、OLE オブジェクトを見つけて抽出する実際の作業を行います。 OLE オブジェクト (DOC、XLS および PDF ファイル) がディスクに保存されます。

// 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();
}
}
}