3

I need to embed an Excel document in a Word document. I used the answer on this SO question -> How can I embed any file type into Microsoft Word using OpenXml 2.0;

Everything works fine except that:

DrawAspect = OVML.OleDrawAspectValues.Icon lets you edit the Excel object by opening a new Excel instance. However, when I edit the data, it is not updated in the Word document.

DrawAspect = OVML.OleDrawAspectValues.Content lets you edit the Excel object directly in the Word document.

My question is, what do I have to change in the code so can I edit the Excel object in the new instance and have it properly reflected in the Word document? I tried everything to no avail.

Something tells me that DrawAspect = OVML.OleDrawAspectValues.Icon suggests that the object acts as an Icon, and changes cannot be properly reflected in this icon.

Community
  • 1
  • 1
jn1kk
  • 5,012
  • 2
  • 45
  • 72

1 Answers1

5

You could try a way I prorosed here:

How to insert an Image in WORD after a bookmark using OpenXML.

In short, use Open XML SDK 2.0 Productivity Tool (which is a part of Open XML SDK 2.0). Do whatever you need to do with document manually in MS Word. Then open this file in Open XML SDK 2.0 Productivity Tool. Then find the edits you are interested in to see how it is represented in OpenXML format , as well as how to do that programmaticaly.

Hope that helps!


UPDATED:


Okay - I have got better now what's the problem is... So in addition to my advice above I would recommend you to look at this threads on MSDN Forum:

I let myself to repost the code sample (posted on MSDN Forum by Ji Zhou) just to avoid the deletion of original thread there.

Hope it is helpful enough to retrieve the Excel object from Word, change some cells and embed it back into Word.

public static void Main()
{
    using (WordprocessingDocument wDoc = WordprocessingDocument.Open(@"D:\test.docx", true))
    {
        Stream stream = wDoc.MainDocumentPart.ChartParts.First().EmbeddedPackagePart.GetStream();
        using (SpreadsheetDocument ssDoc = SpreadsheetDocument.Open(stream, true))
        {
            WorkbookPart wbPart = ssDoc.WorkbookPart;
            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
              Where(s => s.Name == "Sheet1").FirstOrDefault();
            if (theSheet != null)
            {
                Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(theSheet.Id))).Worksheet;
                Cell theCell = InsertCellInWorksheet("C", 2, ws);
                theCell.CellValue = new CellValue("5");
                theCell.DataType = new EnumValue<CellValues>(CellValues.Number);
                ws.Save();
            }
        }
    }
}

private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, Worksheet worksheet)
{
    SheetData sheetData = worksheet.GetFirstChild<SheetData>();
    string cellReference = columnName + rowIndex;
    Row row;
    if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
    {
        row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
    }
    else
    {
        row = new Row() { RowIndex = rowIndex };
        sheetData.Append(row);
    }
    if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
    {
        return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
    }
    else
    {
        Cell refCell = null;
        foreach (Cell cell in row.Elements<Cell>())
        {
            if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
            {
                refCell = cell;
                break;
            }
        }

        Cell newCell = new Cell() { CellReference = cellReference };
        row.InsertBefore(newCell, refCell);
        worksheet.Save();
        return newCell;
    }
}
Community
  • 1
  • 1
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
  • 1
    Since you are the only who replied with somewhat relevant information, I'll mark your post as an answer. However, if you ever work with OpenXML and embedding Objects, the Reflector Tool stores all these objects as binary streams (pre-made). They give you no information how those streams were formed, thus is the problem. – jn1kk Feb 28 '12 at 14:14
  • I have added some more tips about embedded Excel modification. Please have a look whether it helps you. Thanks! – Dmitry Pavlov Feb 28 '12 at 17:52
  • 1
    @DmitryPavlov - thanks, found from your links that the chart data is stored in two locations 1) in the embedded Excel 2) in a cache as a separate XML file. – Kevin Swann Feb 05 '23 at 20:07