Estrarre oggetti OLE dal file di lavoro

Estrarre oggetti OLE da un file di lavoro

Creazione di un file di lavoro modello

  1. Creato un documento di lavoro in Microsoft Excel.
  2. Aggiungi un documento di Microsoft Word, un libro di Excel e un documento in formato PDF come oggetti OLE nel primo foglio di lavoro.
Modello di documento con oggetti OLE (OleFile.xls)
todo:image_alt_text

Successivamente estrai gli oggetti OLE e salvali sull’hard disk con i rispettivi tipi di file.

Scarica e installa Aspose.Cells

  1. Scarica Aspose.Cells for .NET.
  2. Installalo sul tuo computer di sviluppo.

Tutti i componenti Aspose, una volta installati, funzionano in modalità di valutazione. La modalità di valutazione non ha limiti di tempo e inserisce solo filigrane nei documenti prodotti.

Crea un Progetto

Avvia Visual Studio.Net e crea un nuova applicazione console. Questo esempio mostrerà un’applicazione console C#, ma è possibile utilizzare anche VB.NET.

  1. Aggiungi Riferimenti
    1. Aggiungi un riferimento al componente Aspose.Cells al tuo progetto, ad esempio aggiungi un riferimento a …\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll

Estrai oggetti OLE

Il codice di seguito effettua il lavoro effettivo di individuare ed estrarre oggetti OLE. Gli oggetti OLE (file DOC, XLS e PDF) vengono salvati su disco.

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