We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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<o:p></o:p>

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);<o:p></o:p>

int i = 0;<o:p></o:p>

//Loop through all shapes<o:p></o:p>

foreach (Shape shape in shapes)<o:p></o:p>

{<o:p></o:p>

if (shape.OleFormat != null)<o:p></o:p>

{<o:p></o:p>

if (!shape.OleFormat.IsLink)<o:p></o:p>

{<o:p></o:p>

//Extract OLE Word object<o:p></o:p>

if (shape.OleFormat.ProgId == “Word.Document.12”)<o:p></o:p>

{<o:p></o:p>

MemoryStream stream = new MemoryStream();<o:p></o:p>

shape.OleFormat.Save(stream);<o:p></o:p>

Document newDoc = new Document(stream);<o:p></o:p>

newDoc.Save(string.Format(@“C:\test\outEmbeded_{0}.html”, i));<o:p></o:p>

i++;<o:p></o:p>

}<o:p></o:p>

//Extract OLE Excel object<o:p></o:p>

if (shape.OleFormat.ProgId == “Excel.Sheet.12”)<o:p></o:p>

{<o:p></o:p>

// Here you can use Aspose.Cells component<o:p></o:p>

// to be able to convert MS Excel files to separate HTML files<o:p></o:p>

}<o:p></o:p>

}

}<o:p></o:p>

}


2) Do required modification using Aspose.Cells
3) 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);<o:p></o:p>

Stream memoryStream = File.OpenRead(MyDir + “Book1.xls”);<o:p></o:p>

Shape oleObject = builder.InsertOleObject(memoryStream, “Excel.Sheet.8”, true, null);<o:p></o:p>

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:
http://www.aspose.com/docs/display/cellsnet/Converting+Worksheet+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,