Gestione degli Oggetti OLE

Introduzione

OLE (Object Linking and Embedding) è il framework Microsoft per una tecnologia di documento composto. In breve, un documento composto è qualcosa come una scrivania che può contenere oggetti visivi e informativi di ogni tipo: testo, calendari, animazioni, suoni, video in movimento, 3D, notizie continuamente aggiornate, controlli, e così via. Ogni oggetto della scrivania è un’entità di programma indipendente che può interagire con un utente e comunicare anche con altri oggetti sulla scrivania.

OLE (Object Linking and Embedding) è supportato da molti programmi diversi ed è utilizzato per rendere il contenuto creato in un programma disponibile in un altro. Ad esempio, puoi inserire un documento di Microsoft Word in Microsoft Excel. Per vedere che tipi di contenuto puoi inserire, fai clic su Oggetto nel menu Inserisci. Solo i programmi installati sul computer e che supportano gli oggetti OLE appaiono nella casella del Tipo di oggetto.

Inserimento di oggetti OLE nel foglio di lavoro

Aspose.Cells supporta l’aggiunta, l’estrazione e la manipolazione degli oggetti OLE nei fogli di lavoro. Per questo motivo, Aspose.Cells ha la classe OleObjectCollection, utilizzata per aggiungere un nuovo oggetto OLE alla lista di collezioni. Un’altra classe, OleObject, rappresenta un oggetto OLE. Ha alcuni membri importanti:

  • La proprietà ImageData specifica i dati dell’immagine (icona) di tipo array di byte. L’immagine verrà visualizzata per mostrare l’oggetto OLE nel foglio di lavoro.
  • La proprietà ObjectData specifica i dati dell’oggetto sotto forma di un array di byte. Questi dati verranno mostrati nel relativo programma quando si fa doppio clic sull’icona dell’oggetto OLE.

L’esempio seguente mostra come aggiungere un/i oggetto(i) OLE in un foglio di lavoro.

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

Estrazione degli oggetti OLE nel Workbook

Nell’esempio seguente viene mostrato come estrarre gli oggetti OLE in un Workbook. L’esempio ottiene diversi oggetti OLE da un file XLS esistente e salva file diversi (DOC, XLS, PPT, PDF, ecc.) in base al tipo di formato file dell’oggetto OLE.

Dopo aver eseguito il codice, possiamo salvare file diversi in base ai rispettivi tipi di formato degli oggetti 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();
}
}

Estrazione del file MOL incorporato

Aspose.Cells supporta l’estrazione di oggetti di tipi non comuni come MOL (file di dati molecolari contenenti informazioni su atomi e legami). Il seguente frammento di codice illustra l’estrazione di un file MOL incorporato e il salvataggio su disco utilizzando questo file di esempio in Excel.

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

Argomenti avanzati