All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.dynamia.zk.reports.GridExporter Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright (C) 2009 - 2019 Dynamia Soluciones IT S.A.S - NIT 900302344-1
 * Colombia - South America
 * All Rights Reserved.
 *
 * DynamiaTools is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL v3) as
 * published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DynamiaTools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DynamiaTools.  If not, see .
 */
package tools.dynamia.zk.reports;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Column;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import tools.dynamia.commons.BeanUtils;
import tools.dynamia.reports.excel.ExcelFileWriter;

import java.io.File;
import java.io.IOException;

public class GridExporter {

    public static void export(Grid grid, String fileName) throws IOException {
        File outfile = File.createTempFile(fileName + "_", ".xlsx");
        ExcelFileWriter efw = new ExcelFileWriter(outfile);
        efw.setShowCellBorders(true);
        exportHeaders(grid, efw);
        exportRows(grid, efw);
        efw.write();
        Filedownload.save(outfile, "application/excel");

    }

    private static void exportRows(Grid grid, ExcelFileWriter efw) {
        for (Component cmp : grid.getRows().getChildren()) {
            if (cmp instanceof Row) {
                efw.newRow();

                for (Component rowcell : cmp.getChildren()) {
                    Object value = getValue(rowcell);
                    efw.addCell(value);
                }
            }
        }

    }

    private static Object getValue(Component rowcell) {
        try {
            return BeanUtils.invokeGetMethod(rowcell, "value");
        } catch (Exception e) {
            try {
                return BeanUtils.invokeGetMethod(rowcell, "label");
            } catch (Exception e2) {
            }

        }
        return "";
    }

    private static void exportHeaders(Grid grid, ExcelFileWriter efw) {
        efw.newRow();
        for (Component cmp : grid.getColumns().getChildren()) {
            if (cmp instanceof Column) {
                Column column = (Column) cmp;
                efw.addCell(column.getLabel());
            }
        }
    }

    private GridExporter() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy