OLE,是Object Linking and Embedding的縮寫,中文譯為“對象連接與嵌入”。在Office文檔的運用中,是指將某個文檔格式作為對象嵌入或以鏈接形式存在另一個文檔格式里。本文就以將word文檔作為對象嵌入Excel工作表為例,講解如何通過后臺運行Java代碼來實現(xiàn)以上操作。
首先,我們需要搭建測試環(huán)境,除了安裝JDK和Intellij IDEA外,還需借助第三方控件。在這里,推薦使用Free Spire.Office for Java控件。通過E-iceblue中文官網(wǎng)獲取安裝包后,解壓找到lib文件夾下的Spire.office.jar,最后將其手動導(dǎo)入IDEA。或者也可以在IDEA中創(chuàng)建Maven倉庫,然后在pom.xml中鍵入以下代碼進行jar包導(dǎo)入。
<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文檔中的數(shù)據(jù)信息圖像,將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) { //創(chuàng)建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(); } }}
提取結(jié)果:
本文發(fā)布于:2023-02-28 21:00:00,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/1677715448100082.html
版權(quán)聲明:本站內(nèi)容均來自互聯(lián)網(wǎng),僅供演示用,請勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除。
本文word下載地址:ole對象(ole對象數(shù)據(jù)類型是什么).doc
本文 PDF 下載地址:ole對象(ole對象數(shù)據(jù)類型是什么).pdf
| 留言與評論(共有 0 條評論) |