Embedded excel sheet in word RRS feed

  • Question

  • Hi all, 


    I am trying to insert a excel sheet (the sheet contains some data) into word. As I know it, one of the ways we can achieve this is by  using AddEmbeddedPackagePart. 

    EmbeddedPackagePart embeddedObjectPart = myDocMainPart.AddEmbeddedPackagePart(@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    embeddedObjectPart.FeedData(File.Open(file, FileMode.Open));
    string rID = myDocMainPart.GetIdOfPart(embeddedObjectPart);
    ImagePart imgPart = myDocMainPart.AddImagePart(ImagePartType.Emf);
    InsertExcelSheet(bkst, rID, myDocMainPart.GetIdOfPart(imgPart));
        private void InsertExcelSheet(BookmarkStart bkStart, string rId, string imgRId)
          Run run = bkStart.Parent.Descendants<Run>().Where(cf => cf.IsAfter(bkStart)).First();
          RunProperties runprop = run.RunProperties;
          run.RunProperties = runprop;
          EmbeddedObject embeddedObject1 = new EmbeddedObject(
                 new V.Shapetype(
                   new V.Stroke() { JoinStyle = V.StrokeJoinStyleValues.Miter },
                   new V.Formulas(
                     new V.Formula() { Equation = "if lineDrawn pixelLineWidth 0" },
                     new V.Formula() { Equation = "sum @0 1 0" },
                     new V.Formula() { Equation = "sum 0 0 @1" },
                     new V.Formula() { Equation = "prod @2 1 2" },
                     new V.Formula() { Equation = "prod @3 21600 pixelWidth" },
                     new V.Formula() { Equation = "prod @3 21600 pixelHeight" },
                     new V.Formula() { Equation = "sum @0 0 1" },
                     new V.Formula() { Equation = "prod @6 1 2" },
                     new V.Formula() { Equation = "prod @7 21600 pixelWidth" },
                     new V.Formula() { Equation = "sum @8 21600 0" },
                     new V.Formula() { Equation = "prod @7 21600 pixelHeight" },
                     new V.Formula() { Equation = "sum @10 21600 0" }),
                   new V.Path() { AllowGradientShape = true, ConnectionPointType = OVML.ConnectValues.Rectangle, AllowExtrusion = false },
                   new OVML.Lock() { Extension = V.ExtensionHandlingBehaviorValues.Edit, AspectRatio = true }
                 ) { Id = "_x0000_t75", CoordinateSize = "21600,21600", Filled = false, Stroked = false, OptionalNumber = 75, PreferRelative = true, EdgePath = "m@4@5l@4@11@9@11@9@5xe" },
                 new V.Shape(
                   new V.ImageData() { Title = "", RelationshipId = imgRId }
                 ) { Id = "_x0000_i1025", Style = "width:184.9pt;height:99.55pt", Ole = null, Type = "#_x0000_t75" },
                 new OVML.OleObject() { Type = OVML.OleValues.Link, ProgId = "Excel.Sheet.12", ShapeId = "_x0000_i1025", DrawAspect = OVML.OleDrawAspectValues.Content, ObjectId = "_1307530183", Id = rId, UpdateMode = OVML.OleUpdateModeValues.Always }
               ) { DxaOriginal = "3701", DyaOriginal = "1994" };

        private void GenerateImageContent(ImagePart imgPart)
          System.IO.Stream data = GetBinaryDataStream(imagePart4Data);
        #region Binary Data
        private System.IO.Stream GetBinaryDataStream(string base64String)
          return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));

    All this works great, but where is the problem. When word opens the file it reads the contents of the image and displays the image (a proxy for the embedded excel file). Only when you double click on the image does it actually change the image to excel, and then after one finishes editing the sheet and when you click out side of the excel, word take a new picture of the excel sheet and displays that.

    This is not good for me because, I make by changes in excel, outside of word and then using the code above, embed the excel into word. Now the second around when I make changes to the excel file and embed the excel file, When opening in word the, the image is of the outdated data.

    To solve this I tried and used linking instead of embedding. But now when I open the file in word, this dialog box keeps popping up. "The document contains links that may refer to other files. So you want to update this document with the data from the linked files?" - The only way I found to to suppress this dialog box is to go to the Office Button --> word Option --> Advanced --> General --> "Update automatic links at open". If I un-check this option I am back to the old problem of the excel image being outdated. Is there any other way to suppress the this dialog box.

    In general I am looking at a way to embed some data contained in a excel sheet into a word document. Such that if I change the data in the excel sheet and then re-build the word document using the SDK 2.0 to incorporate the latest changes in the excel file, when I open word the changes should be reflected (witch out having to double click on the picture to update it) ? Is there any other way to do this? I am even open to options of converting the sheet to a picture. I know that word somehow does this. It takes a snapshot of the sheet as a .emf image. Is there anyway i could do that through code? 

    Does wordprocessingML support altChunk for excel files? Is there a way i could save the excel sheet as an XPS and then embed the XPS into the word file?


    any help and advice will be greatly appreciated....













    Saturday, June 5, 2010 5:20 PM