Samples‎ > ‎NativePeer‎ > ‎

Export shapes from worksheet

import com.jniwrapper.DoubleFloat;
import com.jniwrapper.Int32;
import com.jniwrapper.win32.automation.IDispatch;
import com.jniwrapper.win32.automation.types.BStr;
import com.jniwrapper.win32.automation.types.Variant;
import com.jniwrapper.win32.com.types.LocaleID;
import com.jniwrapper.win32.excel.*;
import com.jniwrapper.win32.excel.ChartObject;
import com.jniwrapper.win32.excel.Shape;
import com.jniwrapper.win32.excel.Shapes;
import com.jniwrapper.win32.excel._Worksheet;
import com.jniwrapper.win32.excel.impl.ChartObjectsImpl;
import com.jniwrapper.win32.jexcel.Application;
import com.jniwrapper.win32.jexcel.ExcelException;
import com.jniwrapper.win32.jexcel.Workbook;
import com.jniwrapper.win32.jexcel.Worksheet;

import java.io.File;

public class ExportShapesSample {
    public static void main(String[] args) {

        Application application = null;
        Workbook workbook = null;
        try {
            application = new Application();
            workbook = application.openWorkbook(new File("C:\\workbookWithShapes.xls"));
            Worksheet worksheet = workbook.getWorksheet(1);

            final _Worksheet worksheetPeer = worksheet.getPeer();
            worksheet.getOleMessageLoop().doInvokeAndWait(new Runnable() {
                public void run() {
                    final Variant unspecified = Variant.createUnspecifiedParameter();
                    final Int32 localeID = new Int32(LocaleID.LOCALE_SYSTEM_DEFAULT);
                    Shapes shapes = worksheetPeer.getShapes();
                    int shapeId = 1;
                    Shape shape = shapes.item(new Variant(shapeId));
                    shape.copy();

                    IDispatch chartObjectDispatch = worksheetPeer.chartObjects(unspecified, localeID);

                    ChartObjectsImpl chartObjects = new ChartObjectsImpl(chartObjectDispatch);
                    ChartObject chartObject = chartObjects.add(new DoubleFloat(0), new DoubleFloat(0), new DoubleFloat(shape.getWidth()), new DoubleFloat(shape.getHeight()));

                    com.jniwrapper.win32.excel._Chart chart = chartObject.getChart();
                    chartObject.activate();
                    chart.paste(unspecified,localeID);

                    BStr fileName = new BStr("C:\\Temp\\shape.gif");
                    Variant filterName = new Variant("gif");
                    Variant interactive = new Variant(false);

                    chart.export(fileName, filterName, interactive);

                    chart.setAutoDelete(false);
                    chart.release();
                    chartObject.setAutoDelete(false);
                    chartObject.release();

                    chartObjects.setAutoDelete(false);
                    chartObjects.release();
                    shapes.setAutoDelete(false);
                    shapes.release();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                workbook.close(false);
            }
            if (application != null) {
                application.close(true);
            }
        }
    }
}
Ĉ
Anna Dolbina,
May 19, 2016, 11:59 PM