Revision history [back]

click to hide/show revision 1
initial version

answered 2014-09-05 11:36:20 +0800

chillworld gravatar image chillworld flag of Belgium

https://github.com/chillw...

Oke,

Had this problem myself and found the solution for it :

Zul:

<button label="Export"  onClick="@command('renderMeshElementAndExport', ref=activitytrainingslb, name='someExcelName')"/>

or

<button label="Export"  onClick="@command('renderMeshElementAndExport', ref=activitytrainingslb)"/>

Java:

@Command
public void renderMeshElementAndExport(@ContextParam(ContextType.BINDER) Binder binder, @BindingParam("ref") MeshElement meshElement,
        @BindingParam("name") String name) throws Exception {
    if (meshElement instanceof Grid) {
        ((Grid) meshElement).renderAll();
    } else if (meshElement instanceof Listbox) {
        ((Listbox) meshElement).renderAll();
    }
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("ref", meshElement);
    args.put("name", name);
    binder.postCommand("exportMeshElementToExcel", args);
}

@Command
public void exportMeshElementToExcel(@BindingParam("ref") MeshElement meshElement, @BindingParam("name") String name)
        throws Exception {
    if (meshElement instanceof Listbox || meshElement instanceof Grid) {
        if (name == null) {
            name = meshElement.getParent().getPage().getTitle() == null ? "excelsheet" : meshElement.getParent().getPage().getTitle();
        }
        ByteArrayOutputStream out = export(meshElement);
        logger.debug("file size : " + out.size());
        AMedia amedia = new AMedia(name + ".xlsx", "xls", "application/file",
                out.toByteArray());
        Filedownload.save(amedia);
        IOUtils.closeQuietly(out);
    } else {
        throw new IllegalArgumentException("We can only export grid or listbox");
    }
}

private ByteArrayOutputStream export(MeshElement meshElement) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ExcelExporter exporter = new ExcelExporter();
    exporter.export(meshElement, out);
    return out;
}

Oke,

Had this problem myself and found the solution for it :

Zul:

<button label="Export"  onClick="@command('renderMeshElementAndExport', ref=activitytrainingslb, ref=idListboxOrGrid, name='someExcelName')"/>

or

<button label="Export"  onClick="@command('renderMeshElementAndExport', ref=activitytrainingslb)"/>
ref=idListboxOrGrid)"/>

Java:

@Command
public void renderMeshElementAndExport(@ContextParam(ContextType.BINDER) Binder binder, @BindingParam("ref") MeshElement meshElement,
        @BindingParam("name") String name) throws Exception {
    if (meshElement instanceof Grid) {
        ((Grid) meshElement).renderAll();
    } else if (meshElement instanceof Listbox) {
        ((Listbox) meshElement).renderAll();
    }
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("ref", meshElement);
    args.put("name", name);
    binder.postCommand("exportMeshElementToExcel", args);
}

@Command
public void exportMeshElementToExcel(@BindingParam("ref") MeshElement meshElement, @BindingParam("name") String name)
        throws Exception {
    if (meshElement instanceof Listbox || meshElement instanceof Grid) {
        if (name == null) {
            name = meshElement.getParent().getPage().getTitle() == null ? "excelsheet" : meshElement.getParent().getPage().getTitle();
        }
        ByteArrayOutputStream out = export(meshElement);
        logger.debug("file size : " + out.size());
        AMedia amedia = new AMedia(name + ".xlsx", "xls", "application/file",
                out.toByteArray());
        Filedownload.save(amedia);
        IOUtils.closeQuietly(out);
    } else {
        throw new IllegalArgumentException("We can only export grid or listbox");
    }
}

private ByteArrayOutputStream export(MeshElement meshElement) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ExcelExporter exporter = new ExcelExporter();
    exporter.export(meshElement, out);
    return out;
}
Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More