简体   繁体   中英

Can we embed Excel file in ppt through aspose slide java

Can we embed Excel file as a link in ppt through aspose slide java. Currently I have tried with Aspose slide , the object was embedded in the pptx file but while trying to open the file getting exception.please give me some guidelines to functionality.The sample code attached below.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import com.aspose.slides.AutoShape;
import com.aspose.slides.IOleObjectFrame;
import com.aspose.slides.ISlide;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ShapeType;

public class PPTTest1 {

public  void setAsposeLicense() {
    InputStream inputStream = null;
    try {
        License license = new License();
        inputStream = getClass().getClassLoader().getResourceAsStream("C:\\work\\licence\\aspose.slides.lic");
        license.setLicense(inputStream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
        } catch (Exception e) {
            inputStream = null;
        }
    }
}
public static void main(String[] args) throws IOException {
        PPTTest1 ppt=new PPTTest1();
        ppt.setAsposeLicense();
        //Instantiate Prseetation class that represents the PPTX
        Presentation pres = new Presentation();
        //Access the first slide
        ISlide sld = pres.getSlides().get_Item(0);
        //Load an Excel file to Array of Bytes
        File file=new File("C:\\work\\Demo_uploadt.xlsm");
        int length=(int)file.length();
        FileInputStream fstro = new FileInputStream(file);
        byte[] buf = new byte[length];
        fstro.read(buf, 0, length);
        //Add an Ole Object Frame shape\

        byte[] fileContent = Files.readAllBytes(file.toPath());
        IOleObjectFrame ooff = sld.getShapes().insertOleObjectFrame(0,(float)0,(float) 0,100,400, "Excel.Sheet.10", fileContent);
        ooff.setObjectData(fileContent);
        pres.save("C:\\work\\OleEmbed.pptx", SaveFormat.Pptx);
        System.out.println("ppt generated.........");

    }

}

@Bachan Joseph,

I have observed the sample code shared by you and there seems to be no issue in sample code while adding MS Excel OLE object in PowerPoint presentation. There may be issue with source files. Can you please share the source file with which you are getting exception along with stack trace so that we may observe that further to help you.

I am working as support developer in Aspose.

The below code is working fine for me to attach the excel file to a PPT using Aspose slide

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.aspose.slides.IOleEmbeddedDataInfo;
import com.aspose.slides.IOleObjectFrame;
import com.aspose.slides.OleEmbeddedDataInfo;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

public class SetFileTypeForAnEmbeddingObject2 {

    public static void main(String[] args) throws IOException {

        Presentation pres = new Presentation();
        try {
            // Add known Ole objects
            byte[] fileBytes = Files.readAllBytes(Paths.get("C:\\work\\Demo uploadt.xlsm"));

            // Create Ole embedded file info
            IOleEmbeddedDataInfo dataInfo = new OleEmbeddedDataInfo(fileBytes, "xls");

            // Create OLE object
            IOleObjectFrame oleFrame = pres.getSlides().get_Item(0).getShapes().addOleObjectFrame(150, 420, 250, 50,
                    dataInfo);
            oleFrame.setObjectIcon(true);

            pres.save("C:\\work\\" + "SetFileTypeForAnEmbeddingObject7.pptx", SaveFormat.Pptx);
        } finally {
            if (pres != null)
                pres.dispose();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM