Edit and include Excel table into word document

Good evening Aspose Team,

we’re working with Aspose.Total for .NET to evaluate the .Document and .Cells in order to be sure if those libraries fits our demands.

What I’m trying to realize is:

  1. load a word document in memory (OK)
  2. replace some placeholders in the document (OK)
  3. load an excel embedded table inside the document file into memory (OK)
  4. edit a value in a cell and save it back to memory (OK)
  5. replace original embedded excel table with the new one (OK)
  6. open the word document and open the new embedded table (FAIL)

what we see in point 6 is, instead of an Excel table, an image without the possibility to edit as an excel table (in the original document was possible).

Below the code we’re using:

//ms is an already filled MemoryStream
Document doc = new Document(ms);
doc.Range.Replace("@@test@@", "NEWTEXT", new FindReplaceOptions(FindReplaceDirection.Forward));
DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in shapes)
{
    if (shape.OleFormat != null)
    {
        if (!shape.OleFormat.IsLink)
        {
            //Extract OLE Excel object
            if (shape.OleFormat.ProgId == "Excel.Sheet.12")
            {
                MemoryStream stream = new MemoryStream();
                shape.OleFormat.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);

                Workbook book = new Workbook(stream);
                Worksheet sheet = book.Worksheets[0];
                sheet.Replace("@@a1cell@@", "newcontent");

                sheet.PageSetup.PrintArea = "A1: G5";
                //sheet.PageSetup.LeftMargin = 0;
                //sheet.PageSetup.RightMargin = 0;
                //sheet.PageSetup.TopMargin = 0;
                //sheet.PageSetup.BottomMargin = 0;


                book.Save(stream, Aspose.Cells.SaveFormat.Xlsx);

                ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
                imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                imgOptions.OnlyArea = true;

                SheetRender sr = new SheetRender(sheet, imgOptions);
                Bitmap bitmap = sr.ToImage(0);
                builder.MoveTo(shape);
                stream.Position = 0;
                Shape newShape = builder.InsertOleObject(stream, "Excel.Sheet.12", true, bitmap);

                shape.Remove();
                break;
            }
        }
    }
}


doc.Save(output, Aspose.Words.SaveFormat.Docx);
output.Position = 0;

String headerInfo = "attachment; filename =" + "document.docx";
WebOperationContext.Current.OutgoingResponse.Headers["Content - Disposition"] = headerInfo;
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";

return output;

Hi,


Thanks for contacting support.

Can you please share the input files, so that we can test the scenario in our environment (we are unable to replicate the issue using our sample files). We are sorry for this inconvenience.

here it is, is a simple docx file with an embeeded excel table inside.

Let me know!

Hi,


Thanks for sharing the sample file.

It appears that you have shared the output file as it contains the image in resultant DOCX file. As requested earlier, can you please share the input file, so that we can test the scenario in our environment.

no, if you double-click on the excel table you should be able to edit it like an excel table.

Regards

Hi,


Thanks for sharing the details.

I have again tested the scenario using Aspose.Words for .NET 17.3.0 and Aspose.Cells for .NET 17.3.0 in Visual Studio 2010 application running over Windows 7 (x64) and I am unable to notice any issue in resultant DOCX file. As per my observations, the resultant DOCX file contains an excel chart object embedded inside it (rather than an image object). For your reference, I have also attached the output generated over my end.

Hi,

the issue is not the input. The issue is that i am able to access and modify the embedded object but not to write it back to the document after the @cell@@ update.
Let me know
Thanks

lechter86:
the issue is not the input. The issue is that i am able to access and modify the embedded object but not to write it back to the document after the @cell@@ update.
Let me know
Hi,

Thanks for sharing the details.

The reason I stated the scenario of unable to modify the embedded object is because in your first post, you have shared following statement.
lechter86:
6) open the word document and open the new embeeded table (FAIL)

what we see in point 6 is, instead of an excel table, an image without the possibility to edit as an excel table (in the original document was possible).
Anyways, I am also able to notice that Aspose.Cells for .NET is not able to replace “@@a1cell@@” with “newcontent”. The problem has been communicated to respective team and they are further looking into the details of this problem.

As soon as we have some definite updates regarding its resolution, we will let you know. We are sorry for this inconvenience.

Hi,

Thanks for your patience.

We have further investigated the scenario and have observed that following code line contains incorrect match string

sheet.Replace("@@a1cell@@", "newcontent");

In order to perform text replace, you need to update the code line to sheet.Replace("@@cella@@", "newcontent"); as after this change, I am able to see string updated in the document. Please take a look over attached document generated after this correction.