Create custom OLE object

Hi,

I am intended to use Aspose.Cell for Java to insert a custom OLE object type (in this case it’s a AccelrysDraw OLE object) into a specific cell of the Worksheet. From the example here http://www.aspose.com/documentation/java-components/aspose.cells-for-java/managing-ole-objects.html, it’s quite easy. I also googling around and found that Aspose.Cell can embedded various kinds of OLE object, but all of them are popular one (like, PDF, Excel, Word…), I did not see any example that use Aspose to insert a custom (non-popular) OLE object type. Any hints are welcome!

Thanks.

Hi,


Well, Aspose.Cells for Java can extract Ole Objects in all the main formats e.g PDF, XLS, DOC, PPT, MS Equation etc. But, I think since you need to insert your custom oriented OLE Object, you may try to get the file into byte array to stream it and then set it as Ole Object in the worksheet. I think it may work fine even for your custom oriented Ole Object. See the first example in the document on how to insert and set ole objects in the Excel files:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/managing-ole-objects.html

If you find any issue, please give us your sample code with your custom oriented file (which is set as Ole Object), also provide the input/output file. We can check it soon.

Thank you.

Thank you.

Hi Amjad,

First, I manually created an Excel file and then insert my custom OLE object using MS Excel 2003 (please see the attachment ExcelMOLOLE.xls), then I use Aspose.Cell for Java to inspect OLE object properties. Here is the code I used to inspect my custom OLE object
package mypackage.aspose;

import java.io.FileInputStream;

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

public class ExtractOleInfo {

/
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(“ExcelMOLOLE.xls”);
Workbook wb = new Workbook(fis);
Worksheet sheet = wb.getWorksheets().get(0);
int oleCount = sheet.getOleObjects().getCount();
for (int i = 0; i < oleCount; i ++) {
OleObject oleObject = sheet.getOleObjects().get(i);
System.out.println(“oleObject.getFileType():” + oleObject.getFileType());
System.out.println(“oleObject.getMsoDrawingType():” + oleObject.getMsoDrawingType());
System.out.println(“oleObject.getPlacement():” + oleObject.getPlacement());
System.out.println(“oleObject.getLinkedCell():” + oleObject.getLinkedCell());
System.out.println(“oleObject.getProgID():” + oleObject.getProgID());
System.out.println(“oleObject.getName():” + oleObject.getName());
System.out.println(“oleObject.getImageSourceFullName():” + oleObject.getImageSourceFullName());
}
}
}

Then I tried to re-create the Excel which contain my custom OLE object, here is the code:
package mypackage.aspose;

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

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

public class TestAspose {

/
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//Get the image file.
File file = new File(“sample.jpg”);
//Get the picture into the streams.
byte[] img = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(img);
//Get the excel file into the streams.
file = new File(“test.mol”);
byte[] data = new byte[(int)file.length()];
fis = new FileInputStream(file);
fis.read(data);
//Instantiate a new Workbook.
Workbook wb = new Workbook();
//Get the first worksheet.
Worksheet sheet = wb.getWorksheets().get(0);
//Add an Ole object into the worksheet with the image
//shown in MS Excel.
int oleObjIndex = sheet.getOleObjects().add(14, 3, 200, 220, img);
OleObject oleObj = sheet.getOleObjects().get(oleObjIndex);
//Set embedded ole object data.
oleObj.setObjectData(data);
oleObj.setPlacement(PlacementType.MOVE);
oleObj.setFileType(5);
oleObj.setProgID(“MDLDrawOLE.MDLDrawObject.1”);
oleObj.setName(“OleObject 1”);
wb.save(“test-with-mol-ole.xls”);
}
}

When I open up the Excel file created by Aspose.Cell in Excel 2003, the OLE object DID get inserted with the specified image, but when I double click the OLE object, MS Excel 2003 give me this error: Cannot start the source application for this object
How can I set the correct source application for my custom OLE object?
I’ve also attached the raw data file for my custom OLE object, it is created with the application Accelrys Draw 4.0, you can request an academic license to test it out.

If you need further information, just give me a shout, I will provide as much as I can.

Many thanks for your support!

Hi,

Please try to add following code:

oleObj.setSourceFullName(“test.mol”);

when you set the correct file name extension for the OleObject, it should be able to be opened automatically in ms excel.

Hi mshakeel.faiz,

I followed your hint and it works! But my presentation image for my OLE object seems to float around no matter I change the PlacementType property of the OLE object. What I want is to render the ‘presentation’ image (or the preview image) of my OLE to fill a whole specific cell, i.e when that cell is resized, the presentation image will also be resized to fill that cell.
Is it possible to do this with Apose.Cell?

Many thanks in advance!

Hi,


I think you may try:

oleObj.setPlacement(PlacementType.MOVE_AND_SIZE);


Hi Amjad,

I’ve alread tried that PlacementType, the OLE object is resized and moved if the cell/column is resized, but when I drag the OLE object around, it just move freely. The other two PlacementType (MOVE, FREE_FLOATING) are not helpful either. What I want to achieve is to lock the OLE object into a specific cell, i.e the user cannot move (by dragging) the OLE object outside that specific cell.

Is it possible to use Aspose.Cell API to achieve that?
Many thanks.

Hi,

Please create xls/xlsx file with your desired output manually using Ms-Excel and attache here. We will look how to achieve your desired output programmatically using Aspose.Cells and provide you a sample code.