Re: Update Excel table inside a word document

Hi Team
can you please share sample code on how to update excel sheet which is inside word doc.
thanks

Hi Yatin,

Thanks for your inquiry. Yes, you can meet this requirement using Aspose.Words and Aspose.Cells APIs.

  1. Extract embedded Excel document from Word document and load it inside Aspose.Cells DOM using following code:
Document doc = new Document("in.docx");
// Get collection of shapes
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
int i = 0;
// Loop through all shapes
foreach (Shape shape in shapes)
{
    if (shape.OleFormat != null)
    {
        if (!shape.OleFormat.IsLink)
        {
            // Extract OLE Word object
            if (shape.OleFormat.ProgId == "Word.Document.12")
            {
                MemoryStream stream = new MemoryStream();
                shape.OleFormat.Save(stream);
                Document newDoc = new Document(stream);
                newDoc.Save(string.Format(@"C:\test\outEmbeded_{0}.html", i));
                i++;
            }
            // Extract OLE Excel object
            if (shape.OleFormat.ProgId == "Excel.Sheet.12")
            {
                // Here you can use Aspose.Cells component
                // to be able to convert MS Excel files to separate HTML files
            }
        }
    }
}
  1. Do required modification using Aspose.Cells
  2. Aspose.Words supports insertion of embedded OLE objects in Microsoft Word document. Please see the following examples:

Insert embedded Excel 97-2003 document as iconic OLE object from stream using predefined image:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Stream memoryStream = File.OpenRead(MyDir + "Book1.xls");
Shape oleObject = builder.InsertOleObject(memoryStream, "Excel.Sheet.8", true, null);
doc.Save(MyDir + @"out.docx");

Hope, this helps.

Best regards,

Hi Awais,

I am able to read the embedded xls in word .But after updating the cell values I am not sure how to save the xls back in the word.
The xls has 2 sheets Input and Final . We need to update few values in Input and in the Final sheet based on some formatting the values will be displayed .
In the end the word will be converted to pdf format.

Please see the code below and kindly advice.

if (shape.getOleFormat().getProgId().equals("Excel.Sheet.8"))
{
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    shape.getOleFormat().save(stream);
    InputStream stream1 = new ByteArrayInputStream(stream.toByteArray());
    Workbook xls = new Workbook(stream1);
    Worksheet sheet = xls.getWorksheets().get(0);
    Cells cells = sheet.getCells();
    Cell valueCell = cells.get(0, 1);
    valueCell.setValue("111");
}

Hi
Can you. Please reply as we need to complete a poc urgently.
thanks

Hi Yatin,

Thanks for your inquiry. Embedded excel file in Word document is represented as a Shape node in Aspose.Words’ DOM; you can loop through Shape nodes collection and get reference to the original object, after modifying the excel file using Aspose.Cells, you can insert it as a new OLE object using the following way and of-course after that remove the old Shape:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Stream memoryStream = File.OpenRead(MyDir + "Book1.xls");
Shape oleObject = builder.InsertOleObject(memoryStream, "Excel.Sheet.8", true, null);
doc.Save(MyDir + @"out.docx");

You can insert new Shape after the old Shape using the CompositeNode.InsertAfter method.

Best regards,