Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Aug 22, 2022 6:14 pm

Hello. I can't find how move charts from word to excel. I use Java + Spire.doc. There aren't examples in the website or I don't find. I think that library should have method for work with charts. If there is a way then write to me please.

biskvitka
 
Posts: 8
Joined: Thu Sep 10, 2020 5:47 pm

Tue Aug 23, 2022 6:49 am

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
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Wed Aug 24, 2022 3:23 pm

Sorry I did mistake. I wanted to moved chart from excel to word. It was my question.

biskvitka
 
Posts: 8
Joined: Thu Sep 10, 2020 5:47 pm

Thu Aug 25, 2022 3:55 am

Hello biskvitka,

Thanks for your message.
We have given you the solution in your other letter. If you have any further questions, please feel free to contact us.

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Return to Spire.Doc