Embed word/Msword douments in excel sheet

Hi Team,

Can we embed word documents in excel sheet using the aspose cell library.
Can you let us know whether its possible if so can you share a sample.

Thanks in advance

Hi,

Thanks for your posting and using Aspose.Cells for Java.

Yes, it is possible to embed MS-Word document into MS-Excel document as an Ole object using Aspose.Cells for Java.

Please see the code below.

I have attached the source docx Microsoft Word file which has been embedded as OLE object into output xlsx file. I have also attached the output.xlsx file generated by this code.

Please also see the screenshot for more help.

The code is fully commented so it will not be difficult for you to understand

Java


//This is your image file, you can use any

String imgFile =“D:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Blue hills.jpg”;


//This is your docx file, you can also use Aspose.Words to create docx file programmatically

String docFile =“F:/Shak-Data-RW/Downloads/source.docx”;



//We need the byte[] byte array for both image and docx file to continue

File file = new File(imgFile);

byte[] binaryImg = new byte[(int)file.length()];

FileInputStream fis = new FileInputStream(file);

fis.read(binaryImg);


file = new File(docFile);

byte[] binaryDoc = new byte[(int)file.length()];

fis = new FileInputStream(file);

fis.read(binaryDoc);



//Insert OLE object into a workbook

Workbook workbook = new Workbook();


Worksheet worksheet = workbook.getWorksheets().get(0);


int idxOle = worksheet.getOleObjects().add(4, 4, 200, 200, binaryImg);


OleObject objOle = worksheet.getOleObjects().get(idxOle);


objOle.setObjectData(binaryDoc);

objOle.setProgID(“Document”);

objOle.setSourceFullName(“source.docx”);


workbook.save(“F:/Shak-Data-RW/Downloads/output.xlsx”);

Screenshot:

Hi Faiz,


Thanks for your reply.
I tried executing the code by creating a class.But getting the below error.Where Excel1.java is my class name.Please suggest the way to make it work

Exception in thread “main” java.lang.IncompatibleClassChangeError: Found interface sun.font.FontManager, but class was expected
at com.aspose.cells.a.c.cf.p(Unknown Source)
at com.aspose.cells.a.c.cf.(Unknown Source)
at com.aspose.cells.b.a.b.g.(Unknown Source)
at com.aspose.cells.b.a.b.g.(Unknown Source)
at com.aspose.cells.ou.a(Unknown Source)
at com.aspose.cells.ou.a(Unknown Source)
at com.aspose.cells.ou.a(Unknown Source)
at com.aspose.cells.Worksheet.autoFitRow(Unknown Source)
at com.aspose.cells.jH.a(Unknown Source)
at com.aspose.cells.fA.a(Unknown Source)
at com.aspose.cells.fA.v(Unknown Source)
at com.aspose.cells.fA.a(Unknown Source)
at com.aspose.cells.fz.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.save(Unknown Source)
at com.aspose.cells.Workbook.save(Unknown Source)
at com.americanexpress.pzn.mailbatch.Excell.main(Excell.java:70)


Code:
package com.americanexpress.pzn.mailbatch;

import java.io.File;
import java.io.FileInputStream;

import com.aspose.cells.OleObject;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;

public class Excell {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//This is your image file, you can use any

String imgFile =“D://hi.jpg”;


//This is your docx file, you can also use Aspose.Words to create docx file programmatically

String docFile =“D://testd.docx”;



//We need the byte[] byte array for both image and docx file to continue

File file = new File(imgFile);

byte[] binaryImg = new byte[(int)file.length()];

FileInputStream fis = new FileInputStream(file);

fis.read(binaryImg);


file = new File(docFile);

byte[] binaryDoc = new byte[(int)file.length()];

fis = new FileInputStream(file);

fis.read(binaryDoc);



//Insert OLE object into a workbook

Workbook workbook = new Workbook();


Worksheet worksheet = workbook.getWorksheets().get(0);


int idxOle = worksheet.getOleObjects().add(4, 4, 200, 200, binaryImg);


OleObject objOle = worksheet.getOleObjects().get(idxOle);


objOle.setObjectData(binaryDoc);

objOle.setProgID(“Document”);

objOle.setSourceFullName(“source.docx”);


workbook.save(“D://Book1.xlsx”);

}

}


Hi Team/Faiz,


The issue got resolved with new aspose jar of ver 7.5.2.
Can you let me know whether the below can be done or not.

1.)Adding only document,but not the image as icon to document.i.e just like adding an object to excel manually.
2.)Adding the document to particular cell in the excel sheet.
3.)Adding all the documents in the folder in cells specified like A1-A20.A1 have first doc,A2 has second.

Thanks in advance.

Hi,

Thanks for your posting and using Aspose.Cells for Java.

Well, you will have to specify the image for your ole object to make it work. You can choose the cell in which you want to insert the ole object. This is specified in the worksheet.getOleObjects().add(4, 4, 200, 200, binaryImg) method.

For more information on the ole object, please see the following documentation article for your reference.

http://www.aspose.com/docs/display/cellsjava/Managing+OLE+Objects