none
Replacing an image in a Word doc works but not in Excel RRS feed

  • Question

  • Dear list,

    I have successfully replaced an image in a Word document using How to: Replace a Document Part in an Office Open XML Package by Using the Open XML API. I don't manage to do the same thing in an Excel document.

    My current approach is:

                using (SpreadsheetDocument document = SpreadsheetDocument.Open(file.FullName, true))
                {
                    foreach (var part in document.WorkbookPart.GetPartsOfType<WorksheetPart>())
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(part.GetStream());
                        replaceImages(part, doc, nsManager);
                        doc.Save(part.GetStream(FileMode.Create));
                    }
                }
    and replaceImages() is more or less this:

                protected bool replaceImages(OpenXmlPart mainPart, XmlDocument part, XmlNamespaceManager nsManager)
            {
                // Remember whether an error occurred
                bool errorsOccurred = false;

                // Find images
                List<KeyValuePair<string, string>> idsAndNamesOfImages = findImages(part, nsManager, out errorsOccurred);

                // Replace images, see http://msdn.microsoft.com/en-us/library/bb508298.aspx
                foreach (KeyValuePair<string, string> image in idsAndNamesOfImages)
                {
                    string id = image.Key;
                    string name = image.Value;

                    // See whether there is a replacement image at all
                    string mimeType;
                    string replacement = getReplacementImage(name, out mimeType);
                    if (replacement == null)
                    {
                        continue;
                    }

                    // Delete the old image
                    OpenXmlPart oldPart = mainPart.GetPartById(id);
                    mainPart.DeletePart(oldPart);

                    // Add new image
                    ImagePart newPart;
                    if (mainPart.GetType() == typeof(MainDocumentPart))
                    {
                        newPart = ((MainDocumentPart)mainPart).AddImagePart(ImagePartType.Gif, id);
                    }
                    else if (mainPart.GetType() == typeof(WorksheetPart))
                    {
                        newPart = ((WorksheetPart)mainPart).AddImagePart(ImagePartType.Gif, id);
                    }
                    else
                    {
                        throw new ApplicationException("Fatal error: unknown type to add image to.");
                    }
                    using (FileStream stream = new FileStream(replacement, FileMode.Open))
                    {
                        newPart.FeedData(stream);
                    }
                }

                return errorsOccurred;
            }

    Unfortunately, this does not work.

    Many thanks for any pointers,
    Kaspar
    Thursday, August 6, 2009 4:41 PM

All replies

  • Any idea how to tackle this?

    Is there a concrete image replacement/add example for Excel available? With SDK 2.0, peferrably?
     
    Kaspar
    Tuesday, August 11, 2009 9:43 AM
  • Hm, let me ask differently: Should the code for Word also work for Excel?

    I am really struggling with this and appreciate any help.

    Thanks,
    Kaspar
    Friday, August 21, 2009 2:46 PM