Hello biskvitka,
Thanks for your inquiry.
At present, our Spire.Doc only supports extracting the OLE excel chart from a word document, as shown in the below code. I also attached my testing file for your reference.
If your word document chart inserted by MS Word but wasn't OLE object, as for this kind of chart, sorry our product currently do not support extracting it.
- Code: Select all
//Create document and load file from disk
Document doc = new Document();
doc.loadFromFile("data/OLEs.docx");
//Traverse through all sections of the word document
for (int s = 0; s < doc.getSections().getCount(); s++) {
Section section = doc.getSections().get(s);
//Traverse through all Child Objects in the body of each section
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
DocumentObject obj = section.getBody().getChildObjects().get(i);
//Find the paragraph
if (obj instanceof Paragraph) {
Paragraph par = (Paragraph) obj;
for (int j = 0; j < par.getChildObjects().getCount(); j++) {
DocumentObject o = par.getChildObjects().get(j);
//Check whether the object is OLE
if (o.getDocumentObjectType() == DocumentObjectType.Ole_Object) {
DocOleObject ole = (DocOleObject) o;
String type = ole.getObjectType();
//Check whether the object type is "Excel.Sheet.8"
if ("Excel.Sheet.8".equals(type)) {
byteArrayToFile(ole.getNativeData(), "output/extractOLE.xls");
}
}
}
}
}
}
}
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();
}
Sincerely,
Simple
E-iceblue support team
Login to view the files attached to this post.