Reprot in java – step 3


Add column, Manipulating the column together

Hôm nay mình hướng dẫn các bạn việc add thêm column mới vào trong report, củng như việc xữ lý và thao tác các column với nhau .

Tiếp tục cho step 2 , step 3 chúng ta sẻ quan sát các phần code thay đổi:

package thaihoanghai.wordpress.com.demo;

import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

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

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.PercentageColumnBuilder;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;

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

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

    	TextColumnBuilder<String> itemColumn = col.column("Item", "item", type.stringType());
    	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.");

    	/**
    	 * =============================End New Code======================================================
    	 */

    	ConnectDB connect = new ConnectDB();
    	// Create new report design
		JasperReportBuilder report = DynamicReports.report();
		report
		.setColumnTitleStyle(columnTitleStyle)// set style for column title
		.highlightDetailEvenRows() // high light for line even
		/**
		 *=========================New Code==================================================
		 **/
		.columns(rowNumberColumn,itemColumn,quantityColumn,unitPriceColumn,priceColumn,pricePercColumn)
		/**
		 *=========================End New Code==================================================
		 **/
		.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.show();
			report.toPdf(new FileOutputStream("d:/report.pdf"));
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

}

Chúng ta quan sát các code thay đổi :
TextColumnBuilder : Dùng để tạo 1 column mới cho report và nó có một số method để thể hiện việc tính toán với các column khác or với number bất kì nào đó như sau :

Methods 
Modifier and Type Method and Description
TextColumnBuilder<BigDecimal> add(Number number)

Creates a new column by adding a value to this column instance.
TextColumnBuilder<BigDecimal> add(TextColumnBuilder<? extends Number> column)

Creates a new column by adding a column value to this column instance.
TextColumnBuilder<BigDecimal> divide(int scale,
Number number)

Creates a new column by dividing this column instance with a value.
TextColumnBuilder<BigDecimal> divide(int scale,
TextColumnBuilder<? extends Number> column)

Creates a new column by dividing this column instance with a column value.
String getName()
TextColumnBuilder<BigDecimal> multiply(Number number)

Creates a new column by multiplying this column instance with a value.
TextColumnBuilder<BigDecimal> multiply(TextColumnBuilder<? extends Number> column)

Creates a new column by multiplying this column instance with a column value.
TextColumnBuilder<BigDecimal> subtract(Number number)

Creates a new column by subtracting a value from this column instance.
TextColumnBuilder<BigDecimal> subtract(TextColumnBuilder<? extends Number> column)

Creates a new column by subtracting a column value from this column instance.

 

Hình cảnh kết quả khi run:

d1

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: