Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Sep 25, 2023 6:41 am

Hi Team ,

Please let me know how to insert excel file to the Word document as an OLE object.


Thanks in advance

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Mon Sep 25, 2023 8:48 am

Hi,

Thank you for your inquiry.
I put the complete code below for your referenece:
Code: Select all
   Document doc = new Document();
        //Add paragraph
        Section sec = doc.addSection();
        Paragraph paragraph = sec.addParagraph();
        //Load images and add ole objects to link to Excel documents
        DocPicture picture = new DocPicture(doc);
        picture.loadImage("data/1.png");
        DocOleObject ole = paragraph.appendOleObject("data/1.xlsx",picture, OleObjectType.Excel_Worksheet);
        //Save
        doc.saveToFile("output/AddOLE4.docx",FileFormat.Docx_2013);
        doc.dispose();

If you have any issue, just feel free to contact us.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Mon Sep 25, 2023 9:24 am

//Code to save excel file in drive
FilePath = "D:/folderName";
excelFileName = fileName;
File file = new File(FilePath + "/" + excelFileName);
out = new FileOutputStream(reportFile, false);
workbook.saveToFile(file.getAbsolutePath());

//Added your code
DocPicture picture = new DocPicture(document);
picture.loadImage(file.getAbsolutePath()); //error in this line
DocOleObject ole = paragraph.appendOleObject("data/1.xlsx",picture, OleObjectType.Excel_Worksheet);

//I am getting below mentioned exception

Exception in thread "main" class com.spire.office.packages.sprirla: Unknown format. ---> class com.spire.office.packages.sprtlja: Unknown format.
com.spire.office.packages.spresja.spr┸╂—(jee:87)
com.spire.office.packages.sprofna.<init>(ojg:19)
com.spire.office.packages.sprvgna.spr┭‼(uy:144)
com.spire.doc.fields.DocPicture.spr㈬⃝‡–(Unknown Source)
com.spire.doc.fields.DocPicture.loadImage(Unknown Source)
com.spire.doc.fields.DocPicture.spr┈※(Unknown Source)
com.spire.doc.fields.DocPicture.loadImage(Unknown Source)
com.spire.doc.fields.DocPicture.loadImage(Unknown Source)

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Mon Sep 25, 2023 10:07 am

Hi

Thank you for your more inquiry.

In the picture.loadImage() method, the parameter of it should be the path of the image as an icon , and in the paragraph.appendOleObject( ) method, please modify the first parameter to your excel file path you want to add to word file.
You can refer to the screenshot below:
1.png

If you have any other questions or concerns, please feel free to contact us anytime.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Tue Sep 26, 2023 4:56 am

Thank you for your response .It is working now.

When I clicked on excel icon ,excel sheet will be opened inside Document file(means data will be visible inside document file ).But my requirement is excel sheet should open separately ,it should not open inside document file. Please help us for resolving this issue.

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Tue Sep 26, 2023 8:53 am

Hi

Thank you for your more inquiry.
You can refer to the following figure to modify your code:
lQLPJx3bPdnhc5zMu80FALDxKBoQ4Ya0KgUFyKuXgJ8A_1280_187.png

If you have any issue, just feel free to contact us.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Wed Sep 27, 2023 6:37 am

Hi Team ,

Thank you for your response.It is working now

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Wed Sep 27, 2023 6:57 am

Hi,

Thank you for your feedback.
If you have any question, just feel free to write back. I hope you have a wonderful day.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to Spire.Doc

cron