FieldLink with Excel table

Hi there,

I’m currently using the trial version of Aspose.Words for .NET to get an Excel table into a Word document. At the moment I am using a FieldLink type to achieve this. The Excel table ends up in the Word document but, there are two issues. The first one being that it seems that I must hit “Yes” to update the field when I open up the document in word (this will be an issue as in the long run I will need to be able to save it as a PDF and so I cannot have a situation where the document needs to be opened manually. And secondly, I am unsure of a way to place the FieldLink elsewhere rather than just at the very beginning of the document.

@kyleprowe,

You can load existing Word document into Aspose.Words, and then you can use DocumentBuilder class to move cursor to any valid location using various MoveToXXX methods and insert an embedded OLE object to meet this requirement.

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");

Secondly to display the Table in Word document, you should generate an image preview. 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");

Hope, this helps.