Jump to content
Changes to the Jaspersoft community edition download ×

Dynamic Reports


cmaclellan

Recommended Posts

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Its possible to generate the jasper report dynamically.

you need to create the jasper design dynamically.

Write a method and pass the resultset mata data and a empty jasper design(create a empty jrxml and call it) as a parameters.

 

JRDesignStaticText staticText = null;

JRDesignField designField = null;

JRDesignTextField textField = null;

JRDesignExpression exp = null;

JRBand columnHeader = design.getColumnHeader();

List columnHeaderList = (ArrayList) columnHeader.getChildren();

List designFieldList = (ArrayList) design.getMainDesignDataset()

.getFieldsList();

Map designFieldMap = (HashMap) design.getMainDesignDataset()

.getFieldsMap();

 

JRBand bandDetail = design.getDetail();

List bandDetailList = bandDetail.getChildren();

 

try {

 

int xPos = 0;

int columnCount = rsmd.getColumnCount();

int columnWidth = columnCount * 65 + 50;

design.setColumnWidth(columnWidth);

 

for (int j = 1; j <= columnCount; j++) {

String userFriendlyMessage = null;

String column = rsmd.getColumnName(j);

//String columnType = rsmd.getColumnClassName(j);

 

try{

userFriendlyMessage = resourcebundle.getString(column);

}

catch(MissingResourceException mre)

{

// if this exception is thrown then we know that the resource bundle does not contain key

}

 

if( userFriendlyMessage == null)

{

userFriendlyMessage = column;

}

userFriendlyMessage = userFriendlyMessage.trim();

 

//Setting the Header part of JRXML

staticText = new JRDesignStaticText();

staticText.setText(userFriendlyMessage);

staticText.setKey("staticText-" + j);

staticText.setBold(true);

staticText.setTopBorder(new Byte("1"));

staticText.setBottomBorder(new Byte("1"));

staticText.setLeftBorder(new Byte("1"));

staticText.setRightBorder(new Byte("1"));

staticText.setBottomBorderColor(new Color(0, 0, 0));

staticText.setTopBorderColor(new Color(0, 0, 0));

staticText.setLeftBorderColor(new Color(0, 0, 0));

staticText.setRightBorderColor(new Color(0, 0, 0));

staticText.setForecolor(new Color(255,255,255));

staticText.setBackcolor(new Color(0, 102, 255));

staticText.setMode(JRDesignStaticText.MODE_OPAQUE);

staticText.setPdfFontName("Helvetica-Bold");

staticText.setFontName("SanSerif");

staticText.setFontSize(10);

staticText.setWidth(65);

staticText.setHeight(30);

staticText.setX(xPos);

staticText.setY(0);

staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);

columnHeaderList.add(staticText);

 

//Adding the Design field to JRXML

designField = new JRDesignField();

designField.setName(column);

 

designField.setValueClassName(rsmd.getColumnClassName(j).toString());

//designField.setValueClassName("java.lang.String");

designFieldList.add(designField);

designFieldMap.put(column, designField);

 

//Adding the Detail Part to JRXML

textField = new JRDesignTextField();

exp = new JRDesignExpression();

exp.setValueClassName(rsmd.getColumnClassName(j).toString());

exp.setText("$F{" + column + "}");// $F{CYCLE_NUM}

textField.setKey("textField-" + j);

textField.setBlankWhenNull(true);

textField.setTopBorder(new Byte("1"));

textField.setBottomBorder(new Byte("1"));

textField.setLeftBorder(new Byte("1"));

textField.setRightBorder(new Byte("1"));

textField.setBold(true);

textField.setHeight(19);

textField.setWidth(65);

textField.setX(xPos);

textField.setY(0);

 

if (rsmd.getColumnClassName(j).equalsIgnoreCase(

"java.math.BigDecimal")) {

textField

.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);

} else {

textField

.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);

}

textField.setExpression(exp);

bandDetailList.add(textField);

 

xPos = xPos + 65;

staticText = null;

designField = null;

textField = null;

exp = null;

}

Hope this helps you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...