java excel 插入文件_Java 添加OLE对象到Excel文档

本文介绍通过Java程序添加OLE对象到Excel文档。OLE分为两种形式,一种通过嵌入(Embed),方式,一种通过链接(Link)方式。前者是将对象嵌入到文档中,外部对该对象的更改不影响嵌入操作时的对象;后者是以链接形式来保存对象,外部任何对该对象的更改都会影响文档。这里的对象,支持多种不同类型(如下图),如PDF、Word、Excel、PPT等常见格式的文档类型。

5d5ee040cb06b61dec8fee33fb84f467.png

本文中的Java代码示例以Word文档为对象通过嵌入方式添加到Excel工作表的指定单元格,代码可供参考。

工具:

Free Spire.Office for Java(免费版)

注:jar文件获取可通过

jar导入效果如下:

305244ded1bd352f64bc5bf642aa7dc1.png

Java 代码示例

import com.spire.doc.documents.ImageType;

import com.spire.xls.*;

import com.spire.xls.core.IOleObject;

import com.spire.doc.Document;

import java.awt.*;

import java.awt.image.BufferedImage;

public class AddOLE {

public static void main(String[] args) {

//加载excel文档

Workbook wb = new Workbook();

wb.loadFromFile("test.xlsx");

//获取第一个工作表

Worksheet sheet = wb.getWorksheets().get(0);

//获取Word文档图片

String docx = "sample.docx";

BufferedImage image = GetWordImage(docx);

//插入OLE到工作表指定单元格

IOleObject oleObject = sheet.getOleObjects().add(docx,image,OleLinkType.Embed);//插入指定类型的OLE

oleObject.setLocation(sheet.getCellRange("A1"));//指定单元格

oleObject.setObjectType(OleObjectType.WordDocument);//指定OLE对象类型(这里可支持多种类型)

//保存文档

wb.saveToFile("AddOLE.xlsx",ExcelVersion.Version2010);

wb.dispose();

}

//定义一个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;

}

}

OLE添加效果:

249413bd541d907ccac9266c85054ade.png

(本文完)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Word 中添加 OLE 对象,可以使用 Java 的 COM Bridge 组件,通过调用 Word 的 COM 接口实现。 以下是一个示例代码,演示如何在 Word 中添加 Excel 表格: ```java import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class WordOLE { public static void main(String[] args) { ActiveXComponent word = new ActiveXComponent("Word.Application"); word.setProperty("Visible", new Variant(true)); Dispatch documents = word.getProperty("Documents").toDispatch(); Dispatch document = Dispatch.call(documents, "Add").toDispatch(); Dispatch selection = Dispatch.get(word, "Selection").toDispatch(); // 添加 OLE 对象 Dispatch shapes = Dispatch.get(selection, "Shapes").toDispatch(); Dispatch oleObject = Dispatch.call(shapes, "AddOLEObject", "Excel.Sheet", "C:\\example\\example.xlsx", false, false, "", 0, "", 0).toDispatch(); // 调整 OLE 对象的位置和大小 Dispatch frame = Dispatch.get(oleObject, "Anchor").toDispatch(); Dispatch.call(frame, "Select"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(frame, "Select"); Dispatch.call(oleObject, "Activate"); // 保存并关闭文档 Dispatch.call(document, "SaveAs", "C:\\example\\example.docx"); Dispatch.call(document, "Close", new Variant(false)); Dispatch.call(word, "Quit"); } } ``` 在这个例子中,我们使用了 jacob 库来调用 COM 接口。首先,我们打开 Word 应用程序并创建一个新文档。然后,我们获取当前的选区并在其中添加 OLE 对象。接下来,我们使用选区调整 OLE 对象的位置和大小,然后激活它以确保它能够正常工作。最后,我们保存并关闭文档,并退出 Word 应用程序。 请注意,在此过程中,我们需要提供 Excel 表格的路径和类型。在本例中,我们将路径设置为 "C:\\example\\example.xlsx",并将类型设置为 "Excel.Sheet"。您需要根据您的情况进行更改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值