Friday, 13 December 2013

NextReports: Creating the report programmatically

NextReports can be used to create a report programmatically. In our company, there was a need to have a report automatically created starting from a sql query and then run it using the engine api. Just think you have a set of data and a simple template which informs about layout properties (fonts, colors, padding and so on) and you want to bypass the process of creating a report using the designer.

To achieve this you have to create a Report object.
  Report report = new Report();
report.setName("Report File Name"); 
report.setSql("select ...");
report.setLayout(createLayout(columnNames, "Title"));
The only thing you have to do is to create the ReportLayout:
 public static ReportLayout createLayout(List<String> columnNames, String title) {

ReportLayout reportLayout = new ReportLayout();
int size = columnNames.size();

// create a row in header band with a title cell spanned on the entire row
Band headerBand = reportLayout.getHeaderBand();
List<BandElement> titleRow = new ArrayList<BandElement>();
BandElement titleElement = new BandElement(title);
titleElement.setPadding(new Padding(1,1,1,1));
for (int i = 0; i < size - 1; i++) {
List<List<BandElement>> headerElements = new ArrayList<List<BandElement>>();

// create two rows in detail band:
// first with column names and second with ColumnBandElement cells
Band detailBand = reportLayout.getDetailBand();
List<BandElement> headerNamesRow = new ArrayList<BandElement>(size);
List<bandelement> headerFieldsRow = new ArrayList<BandElement>(size);
for (String column : columnNames) {
BandElement he = new BandElement(column);
he.setPadding(new Padding(1,1,1,1));
BandElement ce = new ColumnBandElement(column);
ce.setPadding(new Padding(1,1,1,1));
List<List<BandElement>> detailElements = new ArrayList<List<BandElement=>>();

// create an empty footer row
Band footerBand = reportLayout.getFooterBand();
List<List<BandElement>> footerElements = new ArrayList<List<BandElement>>();
List<BandElement> footerRow = new ArrayList<BandElement>();
for (int i = 0; i < size; i++) {
footerRow.add(new BandElement(""));


return reportLayout;

This is the method used by "New Report" action from designer. You can choose any template properties to add on your BandElement cells.

After creating the report you can run it using the engine api or you can save it using  any ro.nextreports.engine.util.ReportUtil save methods.

1 comment:

  1. IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble.Final Year Projects for CSE

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining .

    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    The Angular Training covers a wide range of topics including Angular Directives, Angular Services, and Angular programmability.Angular Training