ワークシートから OLE オブジェクトを抽出
Contents
[
Hide
]
可能な使用シナリオ
Aspose.Cells を使用すると、ワークシートからあらゆる種類の OLE オブジェクトを抽出できます。Worksheet->GetOleObjects() メソッドを使用して、ワークシート内のすべての OLE オブジェクトにアクセスできます。各 OLE オブジェクトには ProgID と ObjectData プロパティがあり、OLE オブジェクトのタイプを特定し、正常に抽出するのに役立ちます。
ワークシートからOLEオブジェクトを抽出する
次のサンプルコードでは、3つのOLEオブジェクトを含むサンプルExcelファイルをロードします。コードはOLEオブジェクトの種類を識別し、次のファイルとして1つずつ抽出します。
サンプルコード
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-C | |
Aspose::Cells::Startup(); | |
// Source directory path. | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
U16String sampleExtractingOLEObjectsFromWorksheet = srcDir + u"sampleExtractingOLEObjectsFromWorksheet.xlsx"; | |
// Load sample Excel file containing OLE objects. | |
Workbook workbook(sampleExtractingOLEObjectsFromWorksheet); | |
// Get the first worksheet. | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Access the count of Ole objects. | |
int oleCount = worksheet.GetOleObjects().GetCount(); | |
// Iterate all the Ole objects and save to disk with correct file format extension. | |
for (int i = 0; i < oleCount; i++) | |
{ | |
// Access Ole object. | |
OleObject oleObj = worksheet.GetOleObjects().Get(i); | |
// Access the Ole ProgID. | |
U16String strProgId = oleObj.GetProgID(); | |
// Find the correct file extension. | |
U16String fileExt = u""; | |
if (strProgId == u"Document") | |
{ | |
fileExt = u".docx"; | |
} | |
else if (strProgId ==u"Presentation") | |
{ | |
fileExt = u".pptx"; | |
} | |
else if (strProgId == u"Acrobat Document") | |
{ | |
fileExt = u".pdf"; | |
} | |
// Find the correct file name with file extension. | |
U16String fileName = outDir + u"outputExtractOleObject" + fileExt; | |
// Write the Ole object data with correct file name. | |
std::ofstream fout(fileName.ToUtf8(), std::ios::binary | std::ios::out); | |
if (!fout.is_open()) { | |
std::cerr << "Failed to open the file: " << fileName.ToUtf8() << std::endl; | |
continue; | |
} | |
const Vector<uint8_t>& data = oleObj.GetObjectData(); | |
fout.write(reinterpret_cast<char*>(data.GetData()), data.GetLength()); | |
fout.close(); | |
}//for | |
Aspose::Cells::Cleanup(); |