本文将介绍如何使用spire.xls for java提取excel中的ole对象。
import com.spire.xls.*;
import com.spire.xls.core.ioleobject;
import java.io.*;
public class extractoleobjects {
public static void main(string[] args){
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("oleobjectsexample.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文档
case worddocument:
bytearraytofile(object.getoledata(), "output/extractole.docx");
break;
//powerpoint文档
case powerpointslide:
bytearraytofile(object.getoledata(), "output/extractole.pptx");
break;
//pdf文档
case adobeacrobatdocument:
bytearraytofile(object.getoledata(), "output/extractole.pdf");
break;
//excel文档
case excelworksheet:
bytearraytofile(object.getoledata(), "output/extractole.xlsx");
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, false));) {
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();
}
}
}
提取结果: