(JXLS) How to invoke a specific method of the object while generating an excel from template?

75 Views Asked by At

I want to generate an excel from template. Currently I am using jxls-2.14.0, jxls-poi-2.14.0, jxls-reader-2.1.0, and commons-jexl3-3.3 as recommended in docs. Problem is I can't take a value directly by the field like ${employee.name} couse my fileds are stored in the array. And I can only call them by specific method that takes field's code as a param.

This is what did I so far:

this is my excel template

1

Code that generates excel:

private void generateCustomSavedReport(Logger log) {
        ExcelReportDataTable reportTable = getReportDataTable(resultSet, reportRpc, log);
        if (reportTable != null) {
            PoiContext context = new PoiContext();
            context.putVar("reportItem", reportTable.getRows());

            PoiTransformer transformer = null;
            if (stream != null) {
                transformer = PoiTransformer.createTransformer(inputStream, stream);
            } else {
                transformer = PoiTransformer.createTransformer(inputStream, outputStream);
            }
            ReaderConfig.getInstance().setUseDefaultValuesForPrimitiveTypes(true);
            try {
                JxlsHelper.getInstance().processTemplate(context, transformer);
            } catch (IOException e) {
                log.error(e);
            }
        }
    }

reportTable.getRows() retrieves LinkedList<ExcelReportDataRow>. And this is the POJO that I should use:

public final class ExcelReportDataRow {

    private ExcelReportDataTable parent;
    private String[] values;

    public ExcelReportDataRow() {
    }

    public ExcelReportDataRow(ExcelReportDataTable dataTable) {
        parent = dataTable;
        values = new String[parent.getColumnCount()];
    }

    public String[] getRowValue() {
        return values;
    }

    public String getValue(Integer index) {
        return values[index];
    }

    public String getStringValue(String columnName) {
        columnName = columnName.replace("_", ".").replace("+", "_");
        int index = parent.getColumnIndex(columnName);
        if (index != -1) {
            String value = values[index];
            if (value != null && !value.equals("")) {
                return value;
            }
        }
        return "n/a";
    }
}

In Excel, I sat properties like ${report.getStringValue("t.employeename")}. Currently, it is generating an Excel with empty rows. Whether or not anybody faced such a problem? Any help or suggestions are appreciated.

0

There are 0 best solutions below