Samples‎ > ‎MS Office‎ > ‎Operations‎ > ‎

Excel Print Handler

package operations;

import com.jniwrapper.Parameter;
import com.jniwrapper.win32.automation.types.Variant;
import com.jniwrapper.win32.gdi.PrintParameters;
import com.jniwrapper.win32.ole.IOleObject;
import excel.excel.*;
import excel.excel.impl._WorkbookImpl;

public class ExcelPrintHandler extends OfficePrintHandler
{
    public void print()
    {
        Dialog printDialog = getPrintDialog();
        printDialog.show();
    }

    public void print(PrintParameters printParameters)
    {
        Dialog printDialog = getPrintDialog();
        Parameter[] dialogParameters = fillPrintDialogParameters(printParameters);
        printDialog.show(dialogParameters);
    }

    private Dialog getPrintDialog()
    {
        IOleObject oleObject = getContainer().getOleObject();

        _Workbook workbook = new _WorkbookImpl(oleObject);
        _Application application = workbook.getApplication();
        Dialogs dialogs = application.getDialogs();
        return dialogs.getItem(new XlBuiltInDialog(XlBuiltInDialog.xlDialogPrint));
    }

    private Parameter[] fillPrintDialogParameters(PrintParameters printParameters){
        Variant unspecifiedParameter = Variant.createUnspecifiedParameter();

        Parameter[] dialogParameters = new Parameter[]{unspecifiedParameter,
                new Variant(printParameters.getFromPage()),     //From
                new Variant(printParameters.getToPage()),       //To
                new Variant(printParameters.getNumCopies()),    //Copies
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,
                unspecifiedParameter,                           //Print to file
                new Variant(printParameters.isCollate())        //Collate
        };

        return dialogParameters;
    }

}