News Category

Insert OLE Object in Excel in Java applications

2021-04-20 09:05:46 Written by  support iceblue
Rate this item
(0 votes)

Spire.XLS for Java supports to insert Word, Excel, PowerPoint slide and PDF as linked object or embedded object into Excel Worksheet. This article will show you how to insert a Word document as an embedded object into Excel by using Spire.XLS for Java in Java applications.

import com.spire.xls.*;
import com.spire.xls.core.IOleObject;
import com.spire.doc.*;
import com.spire.doc.documents.ImageType;
import java.awt.image.BufferedImage;

public class insertOLEObjects {
    public static void main(String[] args) {
        String docFile = "Sample.docx";
        String outputFile = "output/insertOLEObjects_result.xlsx";

        //Load the Excel document
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");
        //Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        //Generate image
        BufferedImage image = GenerateImage(docFile);
        //insert OLE object
        IOleObject oleObject = worksheet.getOleObjects().add(docFile, image, OleLinkType.Embed);
        oleObject.setLocation(worksheet.getCellRange("B4"));
        oleObject.setObjectType(OleObjectType.ExcelWorksheet);
        //Save the file
        workbook.saveToFile(outputFile, ExcelVersion.Version2010);
    }

    private static BufferedImage GenerateImage(String fileName) {

        //Load the sample word document
        Document document = new Document();
        document.loadFromFile(fileName);

        //Save the first page of word as an image
        BufferedImage image = document.saveToImages(0, ImageType.Bitmap);
        return image;
    }
}

Output:

Insert OLE Object in Excel in Java applications

Additional Info

  • tutorial_title:
Last modified on Wednesday, 01 September 2021 02:46