How to create dynamic columns in jasper report



I have a collection of data that I need to display in the report and create .PDF file after that. The problem is the size of collection is not fixed, and I don't know how to create report columns dynamically. For example, a collection of Dates {Date_1, Date_2, ..., Date_n}, each Date_# takes 1 column in the report. Please share with me if there are some ways to do that with Jasper Report.

Another small problem is how to wrap data in 1 cell of the report. I have a column called 'Name', some names are so long that they can't be displayed fully in a cell. Is there any way to wrap it?

Post edited by: greenfield82, at: 2008/04/27 18:47

greenfield82's picture
Joined: Apr 27 2008 - 4:34am
Last seen: 12 years 11 months ago

8 Answers:

hi greenfield82

U can do this by using Velocity framework along with JasperReports.

Refer this link:

For the second problem you have mentioned, there will be an attribute called isStretchWithOverflow for every text field. Set that attribute to true. The text in the text field will get wrapped within the text field in the report

DNV Srikanth
dnvsrikanth's picture
Joined: Jul 22 2007 - 1:29pm
Last seen: 2 years 4 months ago
Sorry for my poor English.
When i faced the same ptoblem (dynamical column) i generateв reports on fly. Here java sample:

private JasperDesign getSubreportDesign( int aSubreportNumber, List<String> aColumnNames )
throws JRException {
JasperDesign jasperDesign = new JasperDesign();

// Dynamic columns
/ columnQtty;
for( int i = 0; i < columnQtty; i++ ) {
staticText = new JRDesignStaticText();
staticText.setX( start_x );
staticText.setY( 0 );
staticText.setWidth( dynamicColumnWidth );
staticText.setHorizontalAlignment( JRAlignment.HORIZONTAL_ALIGN_CENTER );
staticText.setVerticalAlignment( JRAlignment.VERTICAL_ALIGN_MIDDLE );
staticText.setText( convert2HTML(aColumnNames.get( i )) );
setBox( staticText, (i == (columnQtty - 1)) );
staticText.setStyledText( true );
band.addElement( staticText );
start_x += dynamicColumnWidth;

jasperDesign.setColumnHeader( band );

The same manner make detail fields
dima_toxsoft's picture
Joined: May 6 2007 - 9:46pm
Last seen: 5 years 1 week ago
I have run into the same problem, I just created a blog post that goes through solving this issue using a JRBeanCollectionDataSource, and specifying the columns at runtime. Check it out and let me know if you have any questions.
broschb's picture
Joined: Dec 4 2006 - 4:42am
Last seen: 3 years 8 months ago
I agree with Dima,.. I use to generate the report on the fly,.. I make some function that generate some static and field text according to the data query that we want to create,..
amirage_m_b's picture
Joined: Jun 19 2008 - 5:59pm
Last seen: 12 years 10 months ago
I would really appreciate if you guys amirage_m_b and dima_toxsoft post example code with as much detail as possible or post as an attachment.

Looking forward for the example source!!!

Post edited by: pvradhakrishna, at: 2008/06/25 14:24
pvradhakrishna's picture
Joined: Mar 23 2008 - 4:26am
Last seen: 13 years 3 weeks ago

maybe you should try this,..
mm, i'm sorry but i used my mother language, so maybe you don't uderstand some of the functions,.. [file size=3969]
Post edited by: amirage_m_b, at: 2008/06/27 03:33

amirage_m_b's picture
Joined: Jun 19 2008 - 5:59pm
Last seen: 12 years 10 months ago

Hi there, actually there is a Java API that works over JasperReports that attacks exactly the dynamic columns issue. It is called [url=]DynamicJasper

It is pretty well [url=]documented with lots of [url=]examples.

Here you have an example

      FastReportBuilder drb = new FastReportBuilder();
        DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
                .addColumn("Branch", "branch", String.class.getName(),30) // title, property to show, class of the property, width
                .addColumn("Product Line", "productLine", String.class.getName(),50)
                .addColumn("Item", "item", String.class.getName(),50)
                .addColumn("Item Code", "id", Long.class.getName(),20)
                .addColumn("Quantity", "quantity", Long.class.getName(),30)
                .addColumn("Amount", "amount", Float.class.getName(),30)
                .addGroups(2)   // Group by the first two columns
                .addTitle("November 2006 sales report"«»)
                .addSubtitle("This report was generateed at" + new Date())
                .addUseFullPageWidth(true) //make colums to fill the page width
        JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());   
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
        JasperViewer.viewReport(jp);    //finally display the report report   

and this is the results

size=404]  (broken image link}

As you can see, you can also define groups, styles, etc...

Luckily it will meet your needs

Post edited by: djmamana, at: 2008/06/27 03:39

djmamana's picture
Joined: Jul 19 2007 - 1:47pm
Last seen: 13 years 9 months ago

is there a PHP format for this?

ronelvcabrera007's picture
Joined: Aug 30 2016 - 9:21pm
Last seen: 4 years 7 months ago