Word files with embedded worksheets

Hello,
We are considering to use Aspose, but there is a main concern: we need to be able to save an embedded worksheet from a .docx file as a new Excel file, and (after certain operations on the Excel file), to insert the modified worksheet in place of the original (keeping all formatting etc.).
Is this possible with Aspose?

Thank you,
A.

Hi Anita,

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,

Hello again,
Thank you for your response.
I have tried this, and it’s almost perfect.
But I need the resulting embedded worksheet to keep the properties (width, height, formatting etc.) of the original.

When adding the (modified) xls file as an embedded object, Microsoft Word displays an image of the workbook, while Aspose.Words displays some default “Aspose Words for .NET” image. If the user double clicks on the object, the workbook is shown.
Is there a way to show the image of the workbook instead of the default one?

Instead of adding the modified worksheet as a new object, another approach would be to just update the existing (empty) embedded worksheet with the values from the modified one. Is there a way to cast the OleObject as an Aspose.Cells.Worksheet?

Thanks again,
A.

Hi Anita,

Thanks for your inquiry. Before removing the original Shape, you can take and store required property values such as of ShapeBase.Width, ShapeBase.Height into temporary variables and assign them to newly inserted Shape object.

You can convert modified Excel file to image and pass the image to DocumentBuilder.InsertOleObject method:
https://docs.aspose.com/cells/net/convert-excel-to-image/

I am afraid, there is no way that you can use to update existing OLE object using Aspose.Words. Your thread has been linked to the appropriate issue (WORDSNET-10573) and you will be notified as soon as this feature is available. Sorry for the inconvenience.

Best regards,