Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed an Excel graphic in a Word document with OpenXML working with Word 2010 and 2003

I have to implement a Microsoft Word document generator with embed excel graphics in it. One of my constraint is to make my generated docx work both with Microsoft word 2010 and 2003 + compatibility pack.

I didn't managed to make it works for both of them. I can make it works for Word 2010 but the document are not working for 2003 and vice versa.

After several search to make it work for Word 2003 I have added this in my code :

    private static void Word2003(ChartPart importedChartPart, MainDocumentPart mainDocumentPart, Stream fileStream)
    {
        var ext = new ExternalData { Id = "rel" + 5 };
        importedChartPart.ChartSpace.InsertAt(ext, 3);


        var fi = new FileInfo(@"generated.xlsx");
        importedChartPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/package", new Uri(fi.Name, UriKind.Relative), "rel5");

        EmbeddedPackagePart embeddedObjectPart = mainDocumentPart.AddEmbeddedPackagePart(@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        Stream copyStream = new MemoryStream();
        fileStream.CopyTo(copyStream);
        embeddedObjectPart.FeedData(copyStream);
    }

But at this point generated documents don't work with Word 2010. If I delete these two lignes :

 var ext = new ExternalData { Id = "rel" + 5 };
 importedChartPart.ChartSpace.InsertAt(ext, 3);

from previous code it's works for Word 2010 but not for Word 2003.

I have tried several things but I didn't manage to make it work for each case.

You can find this small piece of code here

The prerequisite is a template of Excel file with a Chart and a graphic in it.


Edit : Generated document always works with Microsoft Office 2007 (with the two problematic code lines or not). I'm still seeking for solutions !

like image 659
PuK Avatar asked Sep 14 '12 14:09

PuK


People also ask

How do I move a graph from Excel to Word?

Open a blank worksheet in Excel. Go to Data | Import External Data | Import Data. (In Excel 2007, click the Data tab, click Get External Data, and then select From Text.) Click the text file you want to import, then click Import.


1 Answers

I finally found the solution !

The problem was due to 2 things : I didn't put the External Data correctly and the External relationship was wrong.

This code make it works :

private static void Word2003(ChartPart importedChartPart, MainDocumentPart mainDocumentPart, Stream fileStream)
{
    // Add of the external data id
    ExternalData ext = new ExternalData { Id = "rel" + 5 };
    AutoUpdate autoUpdate = new AutoUpdate{ Val = false};
    ext.Append(autoUpdate);
    importedChartPart.ChartSpace.Append(ext);

    // Set of the relationship
    var fi = new FileInfo(@"generated.xlsx");
    importedChartPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject", new Uri(fi.Name, UriKind.Relative), "rel5");

    // Link to the embedded file
    EmbeddedPackagePart embeddedObjectPart = mainDocumentPart.AddEmbeddedPackagePart(@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    Stream copyStream = new MemoryStream();
    fileStream.CopyTo(copyStream);
    embeddedObjectPart.FeedData(copyStream);
}

Now generated Word document works with Word 2003, 2007 and 2010.

Maybe this will help somebody!

like image 193
PuK Avatar answered Oct 29 '22 16:10

PuK