Showing posts with label OLE. Show all posts
Showing posts with label OLE. Show all posts

Friday, 30 July 2021

Insert an OLE object to an Excel worksheet using Java codes

Recently, I find a free third-party library called Free Spire.Office for Java and it supports inserting Word/Excel/PowerPoint/PDF documents as linked object or embedded object into Excel worksheets. This article will show how to insert a Word document as an embedded object into Excel using Java codes with the help of it.

DEPENDENCY

First of all, we need to download and install JDK and Intellij IDEA to create a development environment. Then we get the package of the free library from the official website, find Spire.Xls.jar in the “lib” folder. Finally, manually add it to IDEA.

Besides, there is another way to add the Jar file, which is using the following codes in the Maven Pom.xml file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.office.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

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 InsertOLE {
public static void main(String[] args) {

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

//Generate image
BufferedImage image = GenerateImage("C:\\Users\\Test1\\Desktop\\Test.docx");
//insert OLE object
IOleObject oleObject = worksheet.getOleObjects().add("C:\\Users\\Test1\\Desktop\\Test.docx", image, OleLinkType.Embed);
oleObject.setLocation(worksheet.getCellRange(
"B4"));
oleObject.setObjectType(OleObjectType.
ExcelWorksheet);
//Save the file
workbook.saveToFile("output/InsertOLE.xlsx", 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






Friday, 26 March 2021

Embed an Excel File in a PowerPoint Document in Java

This tutorial will demonstrate how to insert an Excel file as an OEL object into a PowerPoint document using Java codes. It’s worth mentioning that I used a third-party library called Free Spire.Presentation for Java to accomplish the function above.

Dependency

Before running codes, you need to add Spire.Presentation.jar in the library to your project. You can directly download it from the link, or reference it by using the following Maven configuration.

<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

import com.spire.presentation.FileFormat;
import com.spire.presentation.IOleObject;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.drawing.IImageData;
import javax.imageio.ImageIO;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

public class InsertOEL {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation ppt = new Presentation();
ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

//Load an image file and add it to the image collection of the presentation
File file = new File("C:\\Users\\Test1\\Desktop\\Image.png");
BufferedImage image = ImageIO.read(file);
IImageData oleImage = ppt.getImages().append(image);

//Load an Excel file and convert it to byte[] object
String excelPath = "C:\\Users\\Test1\\Desktop\\Sample.xlsx";
File excelFile = new File(excelPath);
FileInputStream inputStream = new FileInputStream(excelFile);
byte[] data = new byte[(int) excelFile.length()];
inputStream.read(data, 0, data.length);

//Create a Rectangle2D object
Rectangle2D rect = new Rectangle2D.Float(60, 60, image.getWidth(), image.getHeight());

//Insert the Excel file as an OLE object to the first slide
IOleObject oleObject = ppt.getSlides().get(0).getShapes().appendOleObject("excel", data, rect);
oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);
oleObject.setProgId("Excel.Sheet.12");

//Save to another file
ppt.saveToFile("output/InsertOle.pptx", FileFormat.PPTX_2013);
}
}

Output



 

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...