Embedding HSSF(excel) into HSLF(ppt) using apache poi

前端 未结 1 1142
旧时难觅i
旧时难觅i 2020-12-06 20:03

I want to embed the excel sheet into presentation(PPT) using apache poi. how can we do this? If anyones knows, please help me.

1条回答
  •  醉梦人生
    2020-12-06 20:50

    This took me a while to figure out how the parts belong together ...

    The embedding can be done in two ways:

    • by updating an already embedded worksheet
      • Pro: just call ObjectData.get/setData() and your are done
      • Con: what if you want to have more than one OLE object embedded?
    • or you can embed the elements from scratch (see below)

    As usual when I try to figure out, how to implement certain POI features, I'm comparing the results with Libre Office files, in this case a few parts had to be created/modified:

    • in the Powerpoint object ...
      • the binary data of the emebedded object is stored as a root level record. Most of the root records are position dependent, so you need to recalc all their offsets when a new record, e.g. a slide, is created
      • the binary data record is referenced by embedding records inside the Document record
      • ... and to obfuscate it a bit more, this document reference is referenced once more by the actual shape object
    • in the POIFS of the embedded worksheet ...
      • an Ole Stream entry needs to be created
      • and the root node has to have the class-id of the embedded document type
      • apart from that, there are no changes on the embedded workbook object neccessary and the data itself, is a self-contained excel file

    Furthermore I've used the two practical info classes: BiffViewer and POIFSLister.

    As this is just a proof of concept, it is by far from complete. For further modifications on the representation of the embedded elements, you'll need to consult the spec.

    There is still an unsolved issue of creating a preview image for the embedded object. You might want to use a neutral image, which is replaced anyway, as soon as user activates (double-clicks) the ole object ... An alternative would be to use jodconverter, but than the POI approach would be a bit senseless ...

    (tested with POI3.9 / Libre Office 4.0 / MS Excel Viewer / MS Office 2003)

    import java.awt.geom.Rectangle2D;
    import java.io.*;
    import java.lang.reflect.Field;
    
    import org.apache.poi.POIDocument;
    import org.apache.poi.ddf.*;
    import org.apache.poi.hpsf.ClassID;
    import org.apache.poi.hslf.HSLFSlideShow;
    import org.apache.poi.hslf.exceptions.HSLFException;
    import org.apache.poi.hslf.model.*;
    import org.apache.poi.hslf.model.Picture;
    import org.apache.poi.hslf.model.Slide;
    import org.apache.poi.hslf.record.*;
    import org.apache.poi.hslf.usermodel.*;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.usermodel.*;
    import org.apache.poi.poifs.filesystem.*;
    import org.apache.poi.util.*;
    
    public class PoiOleXlsInPpt {
        static final OleType EXCEL97      = new OleType("{00020820-0000-0000-C000-000000000046}");
        static final OleType EXCEL95      = new OleType("{00020810-0000-0000-C000-000000000046}");
        static final OleType WORD97       = new OleType("{00020906-0000-0000-C000-000000000046}");
        static final OleType WORD95       = new OleType("{00020900-0000-0000-C000-000000000046}");
        static final OleType POWERPOINT97 = new OleType("{64818D10-4F9B-11CF-86EA-00AA00B929E8}");
        static final OleType POWERPOINT95 = new OleType("{EA7BAE70-FB3B-11CD-A903-00AA00510EA3}");
    
        static class OleType {
            final String classId;
            OleType(String classId) {
                this.classId = classId;
            }
            ClassID getClassID() {
                ClassID cls = new ClassID();
                byte clsBytes[] = cls.getBytes();
                String clsStr = classId.replaceAll("[{}-]", "");
                for (int i=0; i

    0 讨论(0)
提交回复
热议问题