إدارة كائنات OLE

مقدمة

OLE (Object Linking and Embedding) هي إطار عمل مايكروسوفت لتكنولوجيا المستند المركب. باختصار، المستند المركب هو شيء مثل سطح مكتب العرض الذي يمكن أن يحتوي على كائنات بصرية ومعلوماتية من جميع الأنواع: نص، تقويمات، رسوم متحركة، صوت، فيديو متحرك، ثلاثي الأبعاد، أخبار متجددة باستمرار، تحكمات، وما إلى ذلك. كل كائن على سطح المكتب هو كيان برنامج مستقل يمكنه التفاعل مع مستخدم وأيضا التواصل مع كائنات أخرى على سطح المكتب.

يتم دعم OLE (Object Linking and Embedding) من قِبل العديد من البرامج المختلفة ويتم استخدامه لجعل المحتوى الذي تم إنشاؤه في برنامج متاح في برنامج آخر. على سبيل المثال، يمكنك إدراج مستند Microsoft Word في Microsoft Excel. لمعرفة أنواع المحتوى التي يمكنك إدراجها، انقر على كائن في قائمة إدراج. يظهر البرامج التي تم تثبيتها على الكمبيوتر والتي تدعم كائنات OLE في مربع نوع الكائن فقط.

إدراج كائنات OLE في ورقة العمل

تدعم Aspose.Cells إضافة واستخراج وتلاعب بكائنات OLE في الأوراق العمل. لهذا السبب، لديها Aspose.Cells OleObjectCollection الفئة التي تُستخدم لإضافة كائن OLE جديد إلى قائمة المجموعة. فئة أُخرى، OleObject، تمثل كائن OLE. لها بعض الأعضاء المهمة:

  • تحدد الخاصية ImageData بيانات الصورة (الرمز) من نوع مصفوفة بايت. سيتم عرض الصورة لعرض كائن OLE في ورقة العمل.
  • تحدد الخاصية ObjectData بيانات الكائن بشكل مصفوفة بايت. سيتم عرض هذه البيانات في البرنامج المرتبط بها عند النقر المزدوج على أيقونة كائن OLE.

المثال التالي يوضح كيفية إضافة كائنات OLE إلى ورقة العمل.

// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
// Define a string variable to store the image path.
string ImageUrl = dataDir + "logo.jpg";
// Get the picture into the streams.
FileStream fs = File.OpenRead(ImageUrl);
// Define a byte array.
byte[] imageData = new Byte[fs.Length];
// Obtain the picture into the array of bytes from streams.
fs.Read(imageData, 0, imageData.Length);
// Close the stream.
fs.Close();
// Get an excel file path in a variable.
string path = dataDir + "book1.xls";
// Get the file into the streams.
fs = File.OpenRead(path);
// Define an array of bytes.
byte[] objectData = new Byte[fs.Length];
// Store the file from streams.
fs.Read(objectData, 0, objectData.Length);
// Close the stream.
fs.Close();
// Add an Ole object into the worksheet with the image
// Shown in MS Excel.
sheet.OleObjects.Add(14, 3, 200, 220, imageData);
// Set embedded ole object data.
sheet.OleObjects[0].ObjectData = objectData;
// Save the excel file
workbook.Save(dataDir + "output.out.xls");

استخراج كائنات OLE في الدفتر

المثال التالي يوضح كيفية استخراج كائنات OLE في كتاب العمل. يقوم المثال بالحصول على كائنات OLE مختلفة من ملف XLS موجود ويحفظ ملفات مختلفة (DOC، XLS، PPT، PDF، إلخ) استنادًا إلى نوع تنسيق ملف كائن OLE.

بعد تشغيل الكود، يمكننا حفظ ملفات مختلفة استنادًا إلى أنواع تنسيق كائنات OLE الخاصة بها.

// 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 + "book1.xls");
// 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 + "ole_" + i + ".";
// Specify each file format based on the oleobject format type.
switch (ole.FileFormatType)
{
case FileFormatType.Doc:
fileName += "doc";
break;
case FileFormatType.Xlsx:
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();
ms.Write(ole.ObjectData, 0, ole.ObjectData.Length);
Workbook oleBook = new Workbook(ms);
oleBook.Settings.IsHidden = false;
oleBook.Save(dataDir + "Excel_File" + i + ".out.xlsx");
}
// Create the files based on the oleobject format types.
else
{
FileStream fs = File.Create(fileName);
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length);
fs.Close();
}
}

استخراج ملف MOL المضمن

تدعم Aspose.Cells استخراج كائنات من أنواع نادرة مثل MOL (ملف بيانات جزيئية يحتوي على معلومات حول الذرات والروابط). يوضح المقتطف من الكود التالي استخراج ملف MOL المضمن وحفظه على القرص باستخدام ملف إكسل عيني هذا.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//directories
string SourceDir = RunExamples.Get_SourceDirectory();
string outputDir = RunExamples.Get_OutputDirectory();
Workbook workbook = new Workbook(SourceDir + "EmbeddedMolSample.xlsx");
var index = 1;
foreach (Worksheet sheet in workbook.Worksheets)
{
OleObjectCollection oles = sheet.OleObjects;
foreach (var ole in oles)
{
string fileName = outputDir + "OleObject" + index + ".mol ";
FileStream fs = File.Create(fileName);
fs.Write(ole.ObjectData, 0, ole.ObjectData.Length);
fs.Close();
index++;
}
}

مواضيع متقدمة