Report in Java – Step 2


Basic Style

Sau đây tiếp tục cho part 1 : chúng ta sẻ thiết lập style đơn giản cho report của chúng ta:

Tương tự cho part phần trước . Qua bài này chúng ta thiết lập 1  table như sau cho việc thao tác :

d1

Và input 1 vài record cho table:

d1

package thaihoanghai.wordpress.com.demo;

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

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.Columns;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import static net.sf.dynamicreports.report.builder.DynamicReports.*;

/**
 * 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));
    	//==================================================================================

    	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
		.columns(// add columns
				// Column (Title, Field_Name_From_Database, Data Type)
				//Columns.column("ID Person","id",DataTypes.stringType()),
				Columns.column("Item", "item", DataTypes.stringType()),
				Columns.column("Quantity", "quantity", DataTypes.integerType()),
				Columns.column("Unit Price", "unitprice", DataTypes.bigDecimalType()))
		.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();
		}
    }

}

Kết quả chương trình 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: