Java API to Generate Grid-type XLS Reports
Java API to Generate Grid-type XLS Reports
A grid panel reports data in a grid format . The Java class GridBuilder provides APIs to generate XLS reports in grid format.
1.1 API of class GridBuilder to build a standard XLS grid-typed report
build(final List<DataRecord> records,
final String title,
final List<Map<String, Object>> visibleFields)
PARAMETERS:
records : List of DataRecord objects.
title : String. The title of XLS.
visibleFields : List of Map objects.
JAVA EXAMPLE:
String view = "test-xls-grid-export.axvw";
DataSource dataSource =
DataSourceFactory.loadDataSourceFromFile(view,"reportGridExport_ds");
List<DataRecord> records = dataSource.getRecords();
String fields =
"[{id:'project.project_id',title:'Project Name',isNumeric:false},
{id:'project.status',title:'Project Status', isNumeric:false},
{id:'project.date_start',title:'Date - Requested Start',isNumeric:false},
{id:'project.date_end',title:'Date - Requested End', isNumeric:false}]";
final List<Map<String, Object>> visibleFields =
EventHandlerBase.fromJSONArray(new JSONArray(fields));
com.archibus.ext.report.xls.GridBuilder reportBuilder =
new com.archibus.ext.report.xls.GridBuilder();
//call build API
reportBuilder.build(records, "A grid XLS report", visibleFields);
String fileName = reportBuilder.getFileName();
String url = reportBuilder.getURL();
1.2 API of class GridBuilder to build a custom XLS report
addCustomRows (final int totalRows,
final int totalColumns,
final List<Map<String, Object>> visibleFields
final List<DataRecord> records)
PARAMETERS:
totalRows : integer, the total rows built in XLS.
totalColumns : integer, the total columns built in XLS.
visibleFields : List of Map objects.
records : List of DataRecord objects.
NOTES:
By overwriting this method, developers may add their own total rows into XLS. All the method parameter values would be passed by the core, and developers could overwrite them and write back to XLS.
JAVA EXAMPLE:
1). Create a sub class of the standard class GridBuilder and implement addCustomRows method to add statistics rows into standard XLS report
public class CustomGridBuilder extends GridBuilder {
/**
* Overwrite addCustomRows() to add extra rows into report
*/
@Override
public void addCustomRows(final int totalRows, final int totalColumns,
final List<Map<String, Object>> visibleFields, final List<DataRecord> records) {
int column = 0;
for (Map<String, Object> field : visibleFields) {
final boolean isNumeric = isNumeric(field);
if (isNumeric) {
// Statistics avg, min and max for the numeric field
double avg = 0.00;
double min = 0.00;
double max = 0.00;
final String fieldName = getStringValue("id", field);
for (DataRecord dataRecord : records) {
final Object value = dataRecord.getNeutralValue(fieldName);
if (value != null) {
final double numericValue = Double.parseDouble(StringUtil.notNull(value));
avg += numericValue;
min = Math.min(min, numericValue);
max = Math.max(max, numericValue);
}
}
int counter = (records != null && records.size() > 0) ? records.size() : 1;
avg = avg / counter;
final int decimals = getDecimals(field);
// XXX: add statistics info to certain XLS cells at the bottom of XLS sheet
addCustomRow(totalRows, column, avg, decimals);
addCustomRow(totalRows + 1, column, min, decimals);
addCustomRow(totalRows + 2, column, max, decimals);
} else {
// just color empty cell
writeFieldTitle(totalRows, column, "", this.totalColoring);
writeFieldTitle(totalRows + 1, column, "", this.totalColoring);
writeFieldTitle(totalRows + 2, column, "", this.totalColoring);
}
column++;
}
// titles for Statistics rows
writeFieldTitle(totalRows, 0, "Average", this.totalColoring);
writeFieldTitle(totalRows + 1, 0, "Min", this.totalColoring);
writeFieldTitle(totalRows + 2, 0, "Max", this.totalColoring);
}
}
2). Call CustomGridBuilder class to generate a custom XLS report
String view = "test-custom-xls-grid.axvw";
DataSource dataSource =
DataSourceFactory.loadDataSourceFromFile(view,"abRepmLsadminPropAndBlBench_ds_grid");
List<DataRecord> records = dataSource.getRecords();
String fields = "[{id:'property.pr_id',title:'Property Code', isNumeric:false},"
+ " {id:'property.city_id',title:'City Code', isNumeric:false},"
+ " {id:'property.value_book',title:'Book Value', isNumeric:true},"
+ " {id:'property.value_market',title:'Market Value', isNumeric:true},"
+ "{id:'property.sum_cost_total',title:'Expense Total', isNumeric:true}]";
List<Map<String, Object>> visibleFields =
EventHandlerBase.fromJSONArray(new JSONArray(fields));
//call CustomGridBuilder
com.archibus.ext.report.xls.CustomGridBuilder reportBuilder =
new com.archibus.ext.report.xls.CustomGridBuilder();
reportBuilder.build(records, "Test a customized grid XLS report", visibleFields);
String fileName = reportBuilder.getFileName();
String url = reportBuilder.getURL();