OLE オブジェクトを管理する
紹介
OLE (Object Linking and Embedding) は、Microsoft の複合ドキュメントテクノロジーのフレームワークです。簡単に言うと、複合ドキュメントとは、テキスト、カレンダー、アニメーション、音声、動画、3D、定期的に更新されるニュース、コントロールなど、あらゆる種類の視覚的および情報オブジェクトを含む表示デスクトップのことです。各デスクトップオブジェクトは、ユーザーと相互作用し、他のデスクトップ上の他のオブジェクトとも通信できる独立したプログラムエンティティです。
OLE (Object Linking and Embedding) は、さまざまなプログラムでサポートされており、あるプログラムで作成したコンテンツを別のプログラムで利用できるようにするために使用されます。たとえば、Microsoft Word ドキュメントを Microsoft Excel に挿入することができます。挿入可能なコンテンツの種類を確認するには、挿入 メニューで オブジェクト をクリックします。コンピュータにインストールされ、OLE オブジェクトをサポートするプログラムだけが オブジェクトの種類 ボックスに表示されます。
ワークシートに OLE オブジェクトを挿入
Aspose.Cells は、ワークシートに OLE オブジェクトを追加、取り出し、操作することをサポートしています。このために、Aspose.Cells には新しい OLE オブジェクトをコレクションリストに追加するための OleObjectCollection クラスがあります。別のクラスである 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オブジェクトが埋め込まれています。
テンプレートファイルには4つの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ファイルの抽出とそれをディスクに保存する方法を示しています。サンプルExcelファイルを使用しています。
// 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++; | |
} | |
} |