Exemplo n.º 1
0
        /// <summary>
        /// Writes the embedded OLE object from the ObjectPool of the binary file to the OpenXml Package.
        /// </summary>
        /// <param name="ole"></param>
        private void copyEmbeddedObject(OleObject ole, EmbeddedObjectPart part)
        {
            //create a new storage
            StructuredStorageWriter writer = new StructuredStorageWriter();

            // Word will not open embedded charts if a CLSID is set.
            if (ole.Program.StartsWith("Excel.Chart") == false)
            {
                writer.RootDirectoryEntry.setClsId(ole.ClassId);
            }

            //copy the OLE streams from the old storage to the new storage
            foreach (string oleStream in ole.Streams.Keys)
            {
                writer.RootDirectoryEntry.AddStreamDirectoryEntry(oleStream, ole.Streams[oleStream]);
            }

            //write the storage to the xml part
            writer.write(part.GetStream());
        }
Exemplo n.º 2
0
        private static void ExtractFile(EmbeddedObjectPart part, string destinationFolderPath)
        {
            // Determine the file name and destination path of the binary,
            // structured storage file.
            string binaryFileName = Path.GetFileName(part.Uri.ToString());
            string binaryFilePath = Path.Combine(destinationFolderPath, binaryFileName);

            // Ensure the destination directory exists.
            Directory.CreateDirectory(destinationFolderPath);

            // Copy part contents to structured storage file.
            using (Stream partStream = part.GetStream())
                using (FileStream fileStream = File.Create(binaryFilePath))
                {
                    partStream.CopyTo(fileStream);
                }

            // Extract the embedded file from the structured storage file.
            Ole10Native.ExtractFile(binaryFilePath, destinationFolderPath);

            // Remove the structured storage file.
            File.Delete(binaryFilePath);
        }