How to create dynamic columns in jasper report

1

Hi,

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?

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

Attachments: 
greenfield82's picture
Joined: Apr 27 2008 - 4:34am
Last seen: 11 years 3 weeks ago

8 Answers:

0
hi greenfield82


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


Refer this link:

http://www.ibm.com/developerworks/websphere/library/techarticles/0505_olivieri/0505_olivieri.html


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: 6 months 5 days ago
0
Hi!
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
int dynamicColumnWidth = (DEFAULT_COLUMN_WIDTH - (DATE_COLUMN_WIDTH
+ REGISTER_COLUMN_WIDTH + PARALLEL_COLUMN_WIDTH))
/ columnQtty;
int start_x = DATE_COLUMN_WIDTH + REGISTER_COLUMN_WIDTH + PARALLEL_COLUMN_WIDTH;
for( int i = 0; i < columnQtty; i++ ) {
staticText = new JRDesignStaticText();
staticText.setX( start_x );
staticText.setY( 0 );
staticText.setWidth( dynamicColumnWidth );
staticText.setHeight( SUBREPORT_COLUMN_HEADER_HEIGHT );
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: 3 years 1 month ago
0
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.

http://broschb.blogspot.com/2008/05/dynamic-jasper-report-using-crosstab...
broschb's picture
24
Joined: Dec 4 2006 - 4:42am
Last seen: 1 year 9 months ago
0
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: 10 years 11 months ago
0
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!!!

Thanks
Krishna
Post edited by: pvradhakrishna, at: 2008/06/25 14:24
pvradhakrishna's picture
Joined: Mar 23 2008 - 4:26am
Last seen: 11 years 2 months ago
0

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 name=Report_v2.zip 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: 10 years 11 months ago
0

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

It is pretty well [url=http://dynamicjasper.sourceforge.net/docs/getting_started.html]documented with lots of [url=http://dynamicjasper.sourceforge.net/docs/howto.html]examples.

Here you have an example
 

Code:
      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
                .build();       
 
        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

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

djmamana's picture
Joined: Jul 19 2007 - 1:47pm
Last seen: 11 years 10 months ago
0

is there a PHP format for this?

ronelvcabrera007's picture
Joined: Aug 30 2016 - 9:21pm
Last seen: 2 years 8 months ago
Feedback
randomness