OLE Objekte aus Arbeitsmappe extrahieren

OLE-Objekte aus einer Arbeitsmappe extrahieren

Erstellen einer Vorlagearbeitsmappe

  1. Erstellen einer Arbeitsmappe in Microsoft Excel.
  2. Fügen Sie ein Microsoft-Word-Dokument, eine Excel-Arbeitsmappe und ein PDF-Dokument als OLE-Objekte auf dem ersten Arbeitsblatt hinzu.
Vorlagendokument mit OLE-Objekten (OleFile.xls)
todo:image_alt_text

Extrahieren Sie dann die OLE-Objekte und speichern Sie sie auf der Festplatte mit ihren jeweiligen Dateitypen.

Aspose.Cells herunterladen und installieren

  1. Laden Sie Aspose.Cells for .NET herunter.
  2. Installieren Sie es auf Ihrem Entwicklungscomputer.

Alle Aspose-Komponenten arbeiten im Evaluierungsmodus, wenn sie installiert sind. Der Evaluierungsmodus hat kein Zeitlimit und fügt nur Wasserzeichen in erstellte Dokumente ein.

Ein Projekt erstellen

Starten Sie Visual Studio.Net und erstellen Sie eine neue Konsolenanwendung. Dieses Beispiel zeigt eine C#-Konsolenanwendung, aber Sie können auch VB.NET verwenden.

  1. Verweise hinzufügen
    1. Fügen Sie Ihrem Projekt einen Verweis auf die Aspose.Cells-Komponente hinzu, beispielsweise einen Verweis auf…\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll

OLE-Objekte extrahieren

Der folgende Code führt die eigentliche Arbeit des Auffindens und Extrahierens von OLE-Objekten durch. Die OLE-Objekte (DOC-, XLS- und PDF-Dateien) werden auf der Festplatte gespeichert.

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