OLE,是Object Linking and Embedding的缩写,中文译为“对象连接与嵌入”。在Office文档的运用中,是指将某个文档格式作为对象嵌入或以链接形式存在另一个文档格式里。本文就以将word文档作为对象嵌入Excel工作表为例,讲解如何通过后台运行Java代码来实现以上操作。
首先,我们需要搭建测试环境,除了安装JDK和Intellij IDEA外,还需借助第三方控件。在这里,推荐使用Free Spire.Office for Java控件。通过E-iceblue中文官网获取安装包后,解压找到lib文件夹下的Spire.office.jar,最后将其手动导入IDEA。或者也可以在IDEA中创建Maven仓库,然后在pom.xml中键入以下代码进行jar包导入。
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http://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>
代码示例
示例1 插入OLE对象到Excel工作表
import com.spire.xls.*;import com.spire.xls.core.IOleObject;import com.spire.doc.Document;import com.spire.doc.documents.ImageType;import java.awt.*;import java.awt.image.BufferedImage;public class InrtOLE { public static void main(String[] args) { //加载excel文档 Workbook wb = new Workbook(); wb.loadFromFile("C:\Urs\Test1\Desktop\Sample.xlsx"); //获取第一个工作表 Worksheet sheet = wb.getWorksheets().get(0); //获取Word文档图片 String docx = "C:\Urs\Test1\Desktop\InrtOLE.docx"; BufferedImage image = GetWordImage(docx); //插入OLE到工作表指定单元格 IOleObject oleObject = sheet.getOleObjects().add(docx,image,OleLinkType.Embed);//插入Embed类型的OLE oleObject.tLocation(sheet.getCellRange("A3"));//指定单元格 oleObject.tObjectType(OleObjectType.WordDocument);//指定OLE对象类型(这里可支持多种类型) //保存文档 wb.saveToFile("output/AddOLE.xlsx",ExcelVersion.Version2010); wb.dispo(); } //定义一个GetWordImage(string docxFile) 方法获取图片,这里的图片来自于Word文档中的数据信息图像,将OLE对象插入到Excel工作表后,图像将显示在Excel工作表中 static BufferedImage GetWordImage(String docxFile) { //加载Word文档 Document doc = new Document(); doc.loadFromFile(docxFile); //将Word文档的第一页保存为图片 Image olePicture = doc.saveToImages(0, ImageType.Bitmap); return (BufferedImage) olePicture; }}
添加效果:
示例2 提取Excel中的OLE对象
import com.spire.xls.*;import com.spire.xls.core.IOleObject;import java.io.*;public class ExtractOLE { public static void main(String[] args) { //创建Workbook实例 Workbook workbook = new Workbook(); //加载Excel文档 workbook.loadFromFile("C:\Urs\Test1\Desktop\AddOLE.xlsx"); //获取第一张工作表 Worksheet sheet = workbook.getWorksheets().get(0); //提取工作表中的OLE对象 if (sheet.hasOleObjects()) { for (int i = 0; i < sheet.getOleObjects().size(); i++) { IOleObject object = sheet.getOleObjects().get(i); OleObjectType type = sheet.getOleObjects().get(i).getObjectType(); switch (type) { //Word文档 ca WordDocument: byteArrayToFile(object.getOleData(), "output/extractOLE.docx"); break; } } } } public static void byteArrayToFile(byte[] datas, String destPath) { File dest = new File(destPath); try (InputStream is = new ByteArrayInputStream(datas); OutputStream os = new BufferedOutputStream(new FileOutputStream(dest, fal));) { byte[] flush = new byte[1024]; int len = -1; while ((len = is.read(flush)) != -1) { os.write(flush, 0, len); } os.flush(); } catch (IOException e) { e.printStackTrace(); } }}
提取结果:
本文发布于:2023-02-28 21:00:00,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/1677715448100082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:ole对象(ole对象数据类型是什么).doc
本文 PDF 下载地址:ole对象(ole对象数据类型是什么).pdf
留言与评论(共有 0 条评论) |