Report in java – Step 4

Fix Width Column and Group by Column

Tiếp theo chúng ta sẻ đi qua 2 đối tượng.

Chúng ta sẻ fix độ rộng của 1 cột nào đó, và phân nhóm các line theo từ group khác nhau.


import static;
import static;
import static;

import java.awt.Color;
import java.math.BigDecimal;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;

 * Hello world!
public class App
    public static void main( String[] args ) throws Exception
    	// Create variables for work set style for border, background
    	StyleBuilder boldStyle =;
    	StyleBuilder boldCenteredStyle  =;
    	StyleBuilder columnTitleStyle =
    									   .setBackgroundColor(new Color(255, 210, 210));

    	 * ====================New Code ===============================================================

    	TextColumnBuilder<String> itemColumn = col.column("Item", "item", type.stringType()).setStyle(boldStyle);
    	TextColumnBuilder<Integer> quantityColumn = col.column("Quantity", "quantity", type.integerType());
    	TextColumnBuilder<BigDecimal> unitPriceColumn = col.column("Unit Price", "unitprice", type.bigDecimalType());
    	// price = unitPrice * quantity
    	TextColumnBuilder<BigDecimal> priceColumn = unitPriceColumn.multiply(quantityColumn).setTitle("Price");
    	PercentageColumnBuilder pricePercColumn = col.percentageColumn("Price %", priceColumn);
    	TextColumnBuilder<Integer> rowNumberColumn = col.reportRowNumberColumn("No.")
    	* ===================New Code=====================
		 * ===================End=====================
		 * */

    	ConnectDB connect = new ConnectDB();
    	// Create new report design
		JasperReportBuilder report =;
		.setColumnTitleStyle(columnTitleStyle)// set style for column title
		.highlightDetailEvenRows() // high light for line even
		 * ===================New Code=====================
		 * */
		 * ===================End=====================
		 * */
		.title(Components.text("Simple Report")// Show Report title
			   .setHorizontalAlignment(HorizontalAlignment.CENTER))// Set position for title
		.pageFooter(Components.pageXofY().setStyle(boldCenteredStyle)) // show number of page at page footer
		// set datasource with specific query from database
		.setDataSource("select id, item, quantity, unitprice from product",connect.openConnect());

		try {;
			report.toPdf(new FileOutputStream("d:/report.pdf"));
		} catch (Exception e) {


Các bạn chú ý các method như sau trong các đoạn New code:

  • setFixedColumns : thiết lập độ rộng cho column

    public T setFixedColumns(Integer columns)
    This method is used to define the fixed width of a column.
    The width is set to the columns multiplied by width of the
    character m for the font used
    columns – the number of fixed columns >= 0
    a column builder
    IllegalArgumentException – if columns is < 0
  • groupBy : Chúng ta group theo column

    public T groupBy(ValueColumnBuilder<?,?>... groupColumns)
  • groupBy : Chúng ta group theo nhiều column

    public T groupBy(GroupBuilder<?>... groups)


Kết quả sau khi run :



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: