إدراج كائنات OLE في ورقة العمل
سيناريوهات الاستخدام المحتملة
يتيح Aspose.Cells لك إدراج كائن OLE داخل ورقة العمل. يرجى استخدام Worksheet->GetOleObjects()->Add() لهذا الغرض. ستحتاج إلى مصفوفة بايتات صورة ستُستخدم لإدراج كائن OLE داخل ورقة العمل وبايتات بيانات الكائن Ole التي ستكون كائنك الفعلي لإدراج كائن Ole داخل ورقة العمل.
إدراج كائنات OLE في ورقة العمل
The following sample code creates the workbook object and inserts the Ole object inside the first worksheet and saves it as output Excel file. Please see the شعار Aspose used as image bytes and input Excel file used as Ole object data inside the code for reference.
الكود المثالي
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Vector<uint8_t> GetDataFromFile(const char* file) | |
{ | |
// open a file | |
std::ifstream fileStream(file, std::ios::binary); | |
if (!fileStream.is_open()) { | |
std::cerr << "Failed to open the file: " << file << std::endl; | |
return Vector<uint8_t>(0); | |
} | |
// Get file size | |
fileStream.seekg(0, std::ios::end); | |
std::streampos fileSize = fileStream.tellg(); | |
fileStream.seekg(0, std::ios::beg); | |
// Read file contents into uint8_t array | |
uint8_t* buffer = new uint8_t[fileSize]; | |
fileStream.read(reinterpret_cast<char*>(buffer), fileSize); | |
fileStream.close(); | |
Vector<uint8_t>data(buffer, fileSize); | |
delete[] buffer; | |
return data; | |
} | |
...... | |
Aspose::Cells::Startup(); | |
// Source directory path. | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of output Excel file. | |
U16String outputInsertingOLEObjectsIntoWorksheet = outDir + u"outputInsertingOLEObjectsIntoWorksheet.xlsx"; | |
// Instantiate a new workbook. | |
Workbook workbook; | |
// Get the first worksheet. | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Read Image for Ole Object into array of bytes. | |
U16String imagePath = srcDir + u"AsposeLogo.png"; | |
Vector<uint8_t>imageData = GetDataFromFile(imagePath.ToUtf8().c_str()); | |
// Read Ole Object into array of bytes. | |
U16String oleObjectPath = srcDir + u"inputInsertOleObject.xlsx"; | |
Vector<uint8_t> oleObjectData = GetDataFromFile(oleObjectPath.ToUtf8().c_str()); | |
// Add an Ole object into the worksheet with the image. | |
int idx = worksheet.GetOleObjects().Add(2, 2, 200, 220, imageData); | |
// Set the Ole object data. | |
OleObject oleObj = worksheet.GetOleObjects().Get(idx); | |
oleObj.SetObjectData(oleObjectData); | |
// Save the workbook. | |
workbook.Save(outputInsertingOLEObjectsIntoWorksheet); | |
Aspose::Cells::Cleanup(); |