Embed Excel cell range in to word document


#1

Hi Team,

Is there any option to insert embedded or normal link OleObject (excel cell range(“A1:I20”) in word document in C# code.


#2

@PeterFrancis,

Thanks for your query.

This query is more related to Aspose.Words, therefore I have moved it to the respective forum. Your requirement will be addressed soon and feedback will be provided.


#3

@PeterFrancis,

You can use OleFormat.SourceItem property to get or set a string that is used to identify the portion of the source file that is being linked. The default value is an empty string. For example, if the source file is a Microsoft Excel workbook, the SourceItem property might return “Workbook1!R3C1:R4C2” if the OLE object contains only a few cells from the worksheet. Sample code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape obj = builder.InsertOleObject(MyDir + "Book1.xlsx", true, true, null);
obj.OleFormat.SourceItem = "Workbook1!R3C1:R4C2";

doc.Save("D:\\temp\\18.10.docx");

Hope, this helps.


#4

Thanks for the code,
By using this i’m able to embed the excel table but I’m not getting excel table in the document it is providing the Aspose.Words for .net image. please suggest me if any option to get excel table.error.JPG (9.0 KB)


#5

@PeterFrancis,

Aspose.Cells supports converting Excel worksheets to images. A simple solution would be to just Convert Worksheet to Image and then use that preview/thumbnail image in DocumentBuilder.InsertOleObject method as follows:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape obj = builder.InsertOleObject(MyDir + "Book1.xlsx", true, true, Image.FromFile(MyDir + "SheetImage.jpg"));
obj.OleFormat.SourceItem = "Workbook1!R3C1:R4C2";

doc.Save("D:\\temp\\18.10.docx");

#6

I have created image of cellRange and inserted into word document and save the document.
After reopen and change the excel table value and if i press F9 to update the links the output is not getting properly, please suggest me any other option it should work similar to Microsoft dll.


#7

@PeterFrancis,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Input Word and Excel files
  • Please list complete steps that we can follow on our end to reproduce the same problem.
  • Please create a standalone simple console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.


#8

Hi Awais, Sample Project.zip (209.7 KB)

In this sample console, I am trying to copy the table from the second worksheet of my workbook i.e “Holiday”. After the code is being executed, then I open my Word Document which has a pop up saying : “This document contains links that may refer to other files. Do you want to update this document with the data from the linked files?”. If i click on 'yes, then the document gets redirected to the first sheet in the workbook i.e “Student”.

  1. If I am updating the table using F9 key, it is redirecting to the active worksheet and not the source item sheet.
  2. If I double click on the image, it gets redirected to the active worksheet.

My expected output should be:

  1. If I update the table using F9 key in Word Document it should reflect same data as in source sheet.
  2. If I double click on the table in Word Document it should open the excel workbook and activate the source sheet.

Please, help me find the solution for the same.


#9

@PeterFrancis,

We are working on your query and will get back to you soon.


#10

Hi Team,

Any update on my request.


#11

@PeterFrancis,

Thanks for being patient. Your request is in the queue. We will check this scenario on our end and share our findings here with you as soon as possible.


#12

@PeterFrancis,

Please check these sample documents: Documents.zip (86.0 KB)

This problem is not reproducible when using the above documents and the following simple code:

Document doc = new Document(MyDir + "WordSample.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Shape obj = builder.InsertOleObject(MyDir + "SampleOutput.xlsx", true, true, Image.FromFile(MyDir + "Holiday.jpg"));
obj.OleFormat.SourceFullName = MyDir + "SampleOutput.xlsx";
obj.OleFormat.SourceItem = "Holiday!R1C1:R5C3";

doc.Save(MyDir + "18.10.docx");

#13

I have tested this code only thing is image will not getting changed.
But if we made any changes in the excel table, it is not getting updated due to table is a image.

My expected output should be:

  1. If I update the table using F9 key in Word Document it should reflect same data as in source sheet.
  2. If I double click on the table in Word Document it should open the excel workbook and activate the source sheet.

#14

@PeterFrancis,

Please also provide your expected DOCX Word document. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using Microsoft Word. Please also list the complete steps that you performed in MS Word to get the desired output.


#15

Hi Awais, Find attachment zip file Documents.zip (50.8 KB), I have listed the steps what we are following for excel table embedding in word using 2 options (normal linking & embedded linking), with microsoft dll code for the same. Please let me know if any other information you need. Thanks.


#16

@PeterFrancis,

We have logged your requirement/problem in our issue tracking system. Your ticket number is WORDSNET-17599 . We will further look into the details of these scenarios and will keep you updated on the status of the linked issue.


#17

@PeterFrancis,

Please see how OLE embedding works in real.

When user inserts Excel file into MS Word application (regardless of MS Word GUI or Word VBA), MS Word calls Windows API to know application registered in Windows registry for .xls documents. Then MS Word makes several COM (Component Object Model) calls to MS Excel application, inviting it to embed document and draw presentation image. MS Excel application replies for invitation and saves document data and renders presentation image using COM calls again.

This explanation is simplified for understanding reason but you can see the main point i.e. presentation image is rendered by OTHER application.

This is main idea of OLE embedding: container application knows nothing about document being embedded. It can be AutoCad drawing, PDF document, MathCad sheet, my own written application etc.

Since Aspose.Words is cross-platform application and might be running on, for example, Linux server. So, we cannot call for Windows registry, COM, etc, on which OLE embedding is based.


#18

@awais.hafeez,

Thanks for the reply,
Can we have one WebEx meeting/call to discuss the issues in detail, please let us know the process to connect.
We have ‘Aspose’ license for the product.

Appreciate your response on this.


#19

@PeterFrancis,

I am afraid, the main avenue of technical support is through our forums. In case you require further assistance, please provide us complete details here and we will supply you with a suitable solution. Thanks for your understanding.