OLE Nesneleri Yönetme

Giriş

OLE (Object Linking and Embedding), Microsoft’un bileşik bir belge teknolojisi için çerçevesidir. Kısaca, bileşik bir belge türü her türlü görsel ve bilgi nesnesini içerebilen bir masaüstü görüntüsü gibidir: metin, takvimler, animasyonlar, ses, hareketli video, 3D, sürekli güncellenen haberler, kontroller vb. Her masaüstü nesnesi, bir kullanıcıyla etkileşime girebilir ve aynı zamanda masaüstünde bulunan diğer nesnelerle iletişim kurabilir.

OLE (Object Linking and Embedding), birçok farklı programa destek sağlar ve bir programda oluşturulan içeriğin başka bir programa kullanılmasını sağlar. Örneğin, bir Microsoft Word belgesini Microsoft Excel’e ekleyebilirsiniz. Ekleyebileceğiniz içerik türlerini görmek için Ekle menüsünde Nesne‘ye tıklayın. Bilgisayara yüklü olan ve OLE nesneleri destekleyen yalnızca programlar Nesne türü kutusunda görünür.

Çalışsayan Elemanları Çalışsayan Eleman (OLE) Nesnesi Ekleme

Aspose.Cells, çalışsayan elemanları çalışsayan eleman koleksiyon listesine eklemek için kullanılan OleObjectCollection sınıfını destekler, çünkü Aspose.Cells’in bir OleObject nesnesi bulunmaktadır. OleObject, bir çalışsayan elemanı temsil eder. Bazı önemli üyelere sahiptir:

  • ImageData özelliği, ikon olarak gösterilecek görüntü (ikon) verisini bayt dizisi türünde belirtir. Görüntü, çalışsayan elemanı çalışsayan eleman levhasında göstermek için kullanılacaktır.
  • ObjectData özelliği, bayt dizisi biçimindeki nesne verisini belirtir. Bu veri, çalışsayan eleman simgesine çift tıkladığınızda ilgili programda gösterilecektir.

Aşağıdaki örnek, çalışsayan elemanları çalışsayan eleman(lar)ı çalışsayan eleman yapıştırma.

// 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");

Çalışsayan Elemanlar’ın Çalışsayan Elemanları Çıkarma

Aşağıdaki örnek, bir çalışma kitabından çalışsayan elemanları çıkarmayı göstermektedir. Örnek, mevcut bir XLS dosyasından farklı çalışsayan elemanlar alır ve farklı dosyalar (DOC, XLS, PPT, PDF vb.) çalışsayan elemanın dosya biçim türüne dayalı olarak kaydeder.

Kodu çalıştırdıktan sonra, ilgili Çalışsayan Elemanın biçim türlerine dayalı olarak farklı dosyaları kaydedebiliriz.

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

Gömülü MOL Dosyasının Çıkarılması

Aspose.Cells, MOL(Moleküler veri dosyası atomlar ve bağlar hakkında bilgi içeren) gibi nadir türdeki nesneleri çıkarmayı destekler. Aşağıdaki kod parçacığı, gömülü MOL dosyasının çıkarılmasını ve bunun diskte örnek excel dosyası‘ını kullanarak kaydedilmesini göstermektedir.

// 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++;
}
}

Gelişmiş Konular