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 in un Foglio di Lavoro

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

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

L’esempio seguente mostra come aggiungere un oggetto OLE in un foglio di lavoro.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(InsertingOLEObjects.class);
// Get the image file.
File file = new File(dataDir + "logo.jpg");
// Get the picture into the streams.
byte[] img = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(img);
// Get the excel file into the streams.
file = new File(dataDir + "Book1.xls");
byte[] data = new byte[(int) file.length()];
fis = new FileInputStream(file);
fis.read(data);
// Instantiate a new Workbook.
Workbook wb = new Workbook();
// Get the first worksheet.
Worksheet sheet = wb.getWorksheets().get(0);
// Add an Ole object into the worksheet with the image shown in MS Excel.
int oleObjIndex = sheet.getOleObjects().add(14, 3, 200, 220, img);
OleObject oleObj = sheet.getOleObjects().get(oleObjIndex);
// Set embedded ole object data.
oleObj.setObjectData(data);
// Save the excel file
wb.save(dataDir + "tstoleobjects.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.

Ecco lo screenshot del file XLS di esempio, che contiene diversi oggetti OLE incorporati nel primo foglio di lavoro.

Il file di modello contiene quattro oggetti OLE

todo:image_alt_text

Dopo aver eseguito il codice, possiamo salvare file diversi in base ai rispettivi tipi di formato degli oggetti OLE. Di seguito sono riportati gli screenshot di alcuni dei file creati.

Il file XLS estratto

todo:image_alt_text

Il file PPT estratto

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ExtractingOLEObjects.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Get the OleObject Collection in the first worksheet.
OleObjectCollection oles = workbook.getWorksheets().get(0).getOleObjects();
// Loop through all the ole objects and extract each object. in the worksheet.
for (int i = 0; i < oles.getCount(); i++) {
if (oles.get(i).getMsoDrawingType() == MsoDrawingType.OLE_OBJECT) {
OleObject ole = (OleObject) oles.get(i);
// Specify the output filename.
String fileName = dataDir + "tempBook1ole" + i + ".";
// Specify each file format based on the oleformattype.
switch (ole.getFileFormatType()) {
case FileFormatType.DOC:
fileName += "doc";
break;
case FileFormatType.EXCEL_97_TO_2003:
fileName += "Xls";
break;
case FileFormatType.PPT:
fileName += "Ppt";
break;
case FileFormatType.PDF:
fileName += "Pdf";
break;
case FileFormatType.UNKNOWN:
fileName += "Jpg";
break;
default:
fileName += "data";
break;
}
FileOutputStream fos = new FileOutputStream(fileName);
byte[] data = ole.getObjectData();
fos.write(data);
fos.close();
}
}

Estrazione del file MOL incorporato

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the directories.
String sourceDir = Utils.Get_SourceDirectory();
String outputDir = Utils.Get_OutputDirectory();
Workbook workbook = new Workbook(sourceDir + "EmbeddedMolSample.xlsx");
int index = 1;
for (Object obj : workbook.getWorksheets())
{
Worksheet sheet = (Worksheet)obj;
OleObjectCollection oles = sheet.getOleObjects();
for (Object obj2 : oles)
{
OleObject ole = (OleObject)obj2;
String fileName = outputDir + "OleObject" + index + ".mol ";
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(ole.getObjectData());
fos.flush();
fos.close();
index++;
}
}

Argomenti avanzati