1. 程式人生 > >jasperreports 動態報表程式碼

jasperreports 動態報表程式碼

jasperreports是一個用來開發報表的元件,通常和ireport來配合使用,可以快速的構建出漂亮的報表。但是ireport畫出的報表模板過於死板,就是不支援動態的列。現在要自己實現一個報表模板,先參考ireport工具畫出的模板,然後改改,用java程式碼自己實現一個。

       專案組目前就是這麼做的,自己去寫一個模板,一個橫排的,一個豎排的,應為欄位比較多,一般採用豎排的。我們專案組做動態報表沒有用DynamicJasper這個開源的動態報表的元件,原因是我覺得他用起來也不太方便。還有就是時間的問題。

       做了兩個模板,現在備份下來。首先是橫排的模板

Java程式碼
複製程式碼
  1. package  cn.hzmc.dynamicreport.example7;   
  2. import  java.awt.Color;   
  3. import  java.sql.Connection;   
  4. import  java.sql.SQLException;   
  5. import  java.text.SimpleDateFormat;   
  6. import  java.util.Date;   
  7. import  java.util.HashMap;   
  8. import  java.util.Map;   
  9. import  net.sf.jasperreports.engine.JRAlignment;   
  10. import  net.sf.jasperreports.engine.JRException;   
  11. import  net.sf.jasperreports.engine.JRReport;   
  12. import  net.sf.jasperreports.engine.JasperCompileManager;   
  13. import  net.sf.jasperreports.engine.JasperExportManager;   
  14. import  net.sf.jasperreports.engine.JasperFillManager;   
  15. import  net.sf.jasperreports.engine.JasperPrint;   
  16. import  net.sf.jasperreports.engine.JasperReport;   
  17. import  net.sf.jasperreports.engine.base.JRBaseLine;   
  18. import  net.sf.jasperreports.engine.base.JRBasePrintText;   
  19. import  net.sf.jasperreports.engine.design.JRDesignBand;   
  20. import  net.sf.jasperreports.engine.design.JRDesignExpression;   
  21. import  net.sf.jasperreports.engine.design.JRDesignField;   
  22. import  net.sf.jasperreports.engine.design.JRDesignLine;   
  23. import  net.sf.jasperreports.engine.design.JRDesignQuery;   
  24. import  net.sf.jasperreports.engine.design.JRDesignStaticText;   
  25. import  net.sf.jasperreports.engine.design.JRDesignStyle;   
  26. import  net.sf.jasperreports.engine.design.JRDesignTextField;   
  27. import  net.sf.jasperreports.engine.design.JasperDesign;   
  28. import  com.capaa.pipe.config.ReadConfig;   
  29. class  Sample6 {   
  30. @SuppressWarnings ( "deprecation" )   
  31.     Sample6()  throws  Exception {   
  32.         Connection connection = getConnection();   
  33.         JasperDesign jasperDesign =  new  JasperDesign();   
  34.         JRDesignStyle normalStyle = setReportSytle(jasperDesign);   
  35.         JRDesignQuery query =  new  JRDesignQuery();   
  36.         query.setText( "select userid,name,age,sex,password,department from person" );   
  37.         jasperDesign.setQuery(query);          
  38. //addReportFiled(jasperDesign);
  39. // Title
  40.         JRDesignBand band =  new  JRDesignBand();   
  41.         band.setHeight( 20 );   
  42.         JRDesignStaticText staticText =  new  JRDesignStaticText();   
  43.         staticText.setX( 200 );   
  44.         staticText.setY( 0 );   
  45.         staticText.setWidth( 200 );   
  46.         staticText.setHeight( 15 );   
  47.         staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);   //設定文字的對齊方式
  48.         staticText.setStyle(normalStyle);   
  49.         staticText.setText( "杭州美創科技有限公司審計報表 " );   
  50.         band.addElement(staticText);           
  51.         JRDesignLine line =  new  JRDesignLine();        
  52.         line.setX( 0 );   
  53.         line.setY( 19 );   
  54.         line.setWidth( 500 );   
  55.         line.setHeight( 1 );   
  56.         line.setForecolor( new  Color( 0x99 , 0xFF , 0xFF ));   
  57.         band.addElement(line);   
  58.         jasperDesign.setTitle(band);   
  59. // Page header
  60.         band =  new  JRDesignBand();   
  61. //band.setHeight(20);
  62.         band.setHeight( 0 );   
  63.         jasperDesign.setPageHeader(band);   
  64. // Column header
  65.         band =  new  JRDesignBand();   
  66.         band.setHeight( 20 );   
  67. // 開始新增列欄位
  68.         String[] headers = { "USERID" , "NAME" , "AGE" , "SEX" , "PASSWORD" , "DEPARTMENT" };   
  69.         String[] alias = { "USERID" , "NAME" , "AGE" , "SEX" , "PASSWORD" , "DEPARTMENT" };   
  70. int  X =  80 ;   
  71. int  columnHeaderfontSize =  10 ;   
  72. int  fontSize =  8 ;   
  73. int  textHeight =  20 ;   
  74. int  textWidth =  80 ;   
  75. int  detailHeight =  20 ;   
  76.          JRDesignBand detail =  new  JRDesignBand();   
  77.          detail.setHeight(detailHeight);   
  78. for ( int  i =  0 ;i<headers.length;i++){   
  79.             JRDesignStaticText jrstaticText =  new  JRDesignStaticText();     
  80.             jrstaticText.setText(headers[i]);    
  81.             jrstaticText.setFontSize(columnHeaderfontSize);     
  82.             jrstaticText.setHeight(textHeight);     
  83.             jrstaticText.setWidth(textWidth);    
  84. if (i== 0 ){   
  85.                 X= 0 ;   
  86.             } else {   
  87.                 X =  80 ;   
  88.             }   
  89.             jrstaticText.setX(X * i);     
  90.             jrstaticText.setPdfFontName( "STSong-Light" );     
  91.             jrstaticText.setPdfEmbedded( true );     
  92.             jrstaticText.setPdfEncoding( "UniGB-UCS2-H" );     
  93. //          jrstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);  
  94. //          jrstaticText.setLeftBorder(JRBaseLine.PEN_1_POINT);  
  95. //          jrstaticText.setTopBorder(JRBaseLine.PEN_1_POINT);  
  96. //          jrstaticText.setRightBorder(JRBaseLine.PEN_1_POINT);  
  97. //          jrstaticText.setBottomBorder(JRBaseLine.PEN_1_POINT);  
  98.             jrstaticText.setForecolor(Color.blue);   
  99. //  jrstaticText.setStretchType((byte)0);
  100.             band.addElement(jrstaticText);     
  101. // 定義欄位,註冊欄位
  102.             JRDesignField field =  new  JRDesignField();   
  103.             field.setName(alias[i]);   
  104.             field.setValueClass(String. class );   
  105.             jasperDesign.addField(field);   
  106. // add text fields for displaying fields
  107.             JRDesignTextField textField =  new  JRDesignTextField();   
  108.             JRDesignExpression expression =  new  JRDesignExpression();   
  109.             expression.setText( "$F{"  + alias[i] +  "}" );   
  110.             expression.setValueClass(String. class );   
  111.             textField.setExpression(expression);   
  112.             textField.setFontSize(fontSize);   
  113.             textField.setHeight(textHeight);   
  114.             textField.setWidth(textWidth);   
  115.             textField.setX(X * i);   
  116.             textField.setPdfFontName( "STSong-Light" );    
  117.             textField.setPdfEmbedded( true );   
  118.             textField.setPdfEncoding( "UniGB-UCS2-H" );     
  119.             textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);   
  120.             textField.setLeftBorder(JRBaseLine.PEN_1_POINT);   
  121.             textField.setTopBorder(JRBaseLine.PEN_1_POINT);   
  122.             textField.setRightBorder(JRBaseLine.PEN_1_POINT);   
  123.             textField.setBottomBorder(JRBaseLine.PEN_1_POINT);   
  124. //textField.setForecolor(new Color(0x99,0xFF,0xFF));
  125.             textField.setForecolor(Color.blue);   
  126.             textField.setStretchWithOverflow( true );   
  127.             detail.addElement(textField);   
  128.         }   
  129.         jasperDesign.setColumnHeader(band);   
  130. //detail
  131.         jasperDesign.setDetail(detail);   
  132. // Column footer
  133.         band =  new  JRDesignBand();   
  134.         band.setHeight( 10 );   
  135.         jasperDesign.setColumnFooter(band);   
  136. // Page footer
  137.         band =  new  JRDesignBand();   
  138.         band.setHeight( 80 );   
  139.         staticText =  new  JRDesignStaticText();   
  140.         staticText.setX( 0 );    
  141.         staticText.setY( 0 );   
  142.         staticText.setWidth( 100 );   
  143.         staticText.setHeight( 20 );   
  144.         staticText.setPdfFontName( "STSong-Light" );      
  145.         staticText.setPdfEmbedded( true );      
  146.         staticText.setPdfEncoding( "UniGB-UCS2-H" );     
  147.         SimpleDateFormat sdf =  new  SimpleDateFormat( "yyyy/MM/dd" );   
  148.         staticText.setText(sdf.format( new  Date()));   
  149.         band.addElement(staticText);   
  150.         JRDesignTextField textField =  new  JRDesignTextField();   
  151.         textField.setX( 450 );    
  152.         textField.setY( 0 );   
  153.         textField.setWidth( 100 );   
  154.         textField.setHeight( 20 );   
  155.         textField.setPdfFontName( "STSong-Light" );      
  156.         textField.setPdfEmbedded( true );      
  157.         textField.setPdfEncoding( "UniGB-UCS2-H" );     
  158.         JRDesignExpression expression =  new  JRDesignExpression();   
  159.          expression =  new  JRDesignExpression();   
  160.          expression.setValueClass(java.lang.Integer. class );        
  161.          expression.setText( "$V{PAGE_NUMBER}" );   
  162.         textField.setExpression(expression);   
  163.         band.addElement(textField);   
  164.         jasperDesign.setPageFooter(band);   
  165.         System.out.println(expression.getText());   
  166. // Summary
  167.         band =  new  JRDesignBand();   
  168.         band.setHeight( 0 );   
  169. /* **************************************************************** */
  170. /* Here My doubt */
  171. /*  
  172.          * JRDesignChart chart1 = new  
  173.          * JRDesignChart(null,JRChart.CHART_TYPE_LINE); chart1.setHeight(100);  
  174.          * chart1.setBorderColor(Color.red); chart1.setForecolor(Color.BLACK);  
  175.          * chart1.setWidth(200);  
  176.          */
  177. // band.addElement(chart1);
  178.         jasperDesign.setSummary(band);   
  179.         System.out.println( "After Connection" );   
  180.         JasperReport jasperReport = JasperCompileManager   
  181.                 .compileReport(jasperDesign);   
  182.         Map parameters =  new  HashMap();   
  183.         System.out.println( "After Parameter" );   
  184.         JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,   
  185.                 parameters, connection);   
  186.         System.out.println( "After Print" );   
  187.         JasperExportManager.exportReportToHtmlFile(jasperPrint,  "D://test2.html" );   
  188.         JasperExportManager.exportReportToPdfFile(jasperPrint,  "D://test2.pdf" );   
  189.         JasperExportManager.exportReportToXmlFile(jasperPrint,  "D://test2.xml" false );   
  190.         System.out.println( "After Export" );   
  191.     }   
  192. private void  addReportFiled(JasperDesign jasperDesign)  throws  JRException {   
  193.         JRDesignField field;   
  194.         field= new  JRDesignField();   
  195.         field.setName( "USERID" );   
  196.         field.setValueClass(java.math.BigDecimal. class );   
  197.         jasperDesign.addField(field);   
  198.         field =  new  JRDesignField();   
  199.         field.setName( "NAME" );   
  200.         field.setValueClass(java.lang.String. class );   
  201.         jasperDesign.addField(field);   
  202.         field =  new  JRDesignField();   
  203.         field.setName( "AGE" );   
  204.         field.setValueClass(java.lang.String. class );   
  205.         jasperDesign.addField(field);   
  206.         field =  new  JRDesignField();   
  207.         field.setName( "SEX" );   
  208.         field.setValueClass(java.lang.String. class );   
  209.         jasperDesign.addField(field);   
  210.         field =  new  JRDesignField();   
  211.         field.setName( "PASSWORD" );   
  212.         field.setValueClass(java.lang.String. class );   
  213.         jasperDesign.addField(field);   
  214.         field =  new  JRDesignField();   
  215.         field.setName( "DEPARTMENT" );   
  216.         field.setValueClass(java.lang.String. class );   
  217.         jasperDesign.addField(field);   
  218.     }   
  219. private  JRDesignStyle setReportSytle(JasperDesign jasperDesign)   
  220. throws  JRException {   
  221.         jasperDesign.setName( "SampleReport" );   
  222.         jasperDesign.setPageWidth( 595 );   
  223.         jasperDesign.setPageHeight( 842 );   
  224.         jasperDesign.setColumnWidth( 535 );   
  225.         jasperDesign.setColumnSpacing( 0 );   
  226.         jasperDesign.setLeftMargin( 30 );   
  227.         jasperDesign.setRightMargin( 30 );   
  228.         jasperDesign.setTopMargin( 20 );   
  229.         jasperDesign.setBottomMargin( 20 );   
  230. // whenNoDataType="NoPages"
  231.         jasperDesign.setWhenNoDataType(JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE);   
  232. // isTitleNewPage="false"
  233.         jasperDesign.setTitleNewPage( false );   
  234. // isSummaryNewPage="false"
  235.         jasperDesign.setSummaryNewPage( false );   
  236. //  jasperDesign.setOrientation(JRReport.ORIENTATION_PORTRAIT);
  237. //  jasperDesign.setPrintOrder(JRReport.PRINT_ORDER_VERTICAL);
  238.         JRDesignStyle normalStyle =  new  JRDesignStyle();   
  239.         normalStyle.setName( "Arial_Normal" );   
  240.         normalStyle.setDefault( true );   
  241.         normalStyle.setFontName( "Arial" );   
  242.         normalStyle.setFontSize( 12 );   
  243.         normalStyle.setPdfFontName( "STSong-Light" );   
  244.         normalStyle.setPdfEncoding( "UniGB-UCS2-H" );   
  245.         normalStyle.setPdfEmbedded( true );   
  246.         jasperDesign.addStyle(normalStyle);   
  247.         JRDesignStyle boldStyle =  new  JRDesignStyle();   
  248.         boldStyle.setName( "Arial_Bold" );   
  249.         boldStyle.setFontName( "Arial" );   
  250.         boldStyle.setFontSize( 12 );   
  251.         boldStyle.setBold( true );   
  252.         boldStyle.setPdfFontName( "STSong-Light" );   
  253.         boldStyle.setPdfEncoding( "UniGB-UCS2-H" );   
  254.         boldStyle.setPdfEmbedded( true );   
  255.         jasperDesign.addStyle(boldStyle);   
  256.         JRDesignStyle italicStyle =  new  JRDesignStyle();   
  257.         italicStyle.setName( "Arial_Italic" );   
  258.         italicStyle.setFontName( "Arial" );   
  259.         italicStyle.setFontSize( 12 );   
  260.         italicStyle.setItalic( true );   
  261.         italicStyle.setPdfFontName( "STSong-Light" );   
  262.         italicStyle.setPdfEncoding( "UniGB-UCS2-H" );   
  263.         italicStyle.setPdfEmbedded( true );   
  264.         jasperDesign.addStyle(italicStyle);   
  265. return  normalStyle;   
  266.     }   
  267. private  Connection getConnection()  throws  SQLException {   
  268.         Connection connection = ReadConfig.getProduceInstance().getDataSource()   
  269.                 .getConnection();   
  270. return  connection;   
  271.     }   
  272. }   
  273. class  Sample {   
  274. public static void  main(String args[])  throws  Exception {   
  275.         Sample6 obj =  new  Sample6();   
  276.     }   
  277. }  
package cn.hzmc.dynamicreport.example7;

import java.awt.Color;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRAlignment;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseLine;
import net.sf.jasperreports.engine.base.JRBasePrintText;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignLine;
import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignStyle;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;

import com.capaa.pipe.config.ReadConfig;

class Sample6 {
	@SuppressWarnings("deprecation")
	Sample6() throws Exception {
		
		Connection connection = getConnection();
		
		JasperDesign jasperDesign = new JasperDesign();
		JRDesignStyle normalStyle = setReportSytle(jasperDesign);
		
		JRDesignQuery query = new JRDesignQuery();
		query.setText("select userid,name,age,sex,password,department from person");
		jasperDesign.setQuery(query);		
		//addReportFiled(jasperDesign);
		
		// Title
		JRDesignBand band = new JRDesignBand();
		band.setHeight(20);
		JRDesignStaticText staticText = new JRDesignStaticText();
		
		staticText.setX(200);
		staticText.setY(0);
		staticText.setWidth(200);
		staticText.setHeight(15);
		staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);  //設定文字的對齊方式
		staticText.setStyle(normalStyle);
		staticText.setText("杭州美創科技有限公司審計報表 ");
		band.addElement(staticText);		
		JRDesignLine line = new JRDesignLine();		
		line.setX(0);
		line.setY(19);
		line.setWidth(500);
		line.setHeight(1);
		line.setForecolor(new Color(0x99,0xFF,0xFF));
		band.addElement(line);
		jasperDesign.setTitle(band);

		// Page header
		band = new JRDesignBand();
		//band.setHeight(20);
		band.setHeight(0);
		jasperDesign.setPageHeader(band);

		// Column header
		band = new JRDesignBand();
		band.setHeight(20);
		
		 // 開始新增列欄位
		String[] headers = {"USERID","NAME","AGE","SEX","PASSWORD","DEPARTMENT"};
		String[] alias = {"USERID","NAME","AGE","SEX","PASSWORD","DEPARTMENT"};
		 int X = 80;
		 int columnHeaderfontSize = 10;
		 int fontSize = 8;
		 int textHeight = 20;
		 int textWidth = 80;
		 int detailHeight = 20;
		 JRDesignBand detail = new JRDesignBand();
		 detail.setHeight(detailHeight);
		for(int i = 0;i<headers.length;i++){
			JRDesignStaticText jrstaticText = new JRDesignStaticText();  
			jrstaticText.setText(headers[i]); 
			
			jrstaticText.setFontSize(columnHeaderfontSize);  
			jrstaticText.setHeight(textHeight);  
			jrstaticText.setWidth(textWidth); 
			if(i==0){
				X=0;
			}else{
				X = 80;
			}
			jrstaticText.setX(X * i);  
			jrstaticText.setPdfFontName("STSong-Light");  
			jrstaticText.setPdfEmbedded(true);  
			jrstaticText.setPdfEncoding("UniGB-UCS2-H");  
//			jrstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);  
//			jrstaticText.setLeftBorder(JRBaseLine.PEN_1_POINT);  
//			jrstaticText.setTopBorder(JRBaseLine.PEN_1_POINT);  
//			jrstaticText.setRightBorder(JRBaseLine.PEN_1_POINT);  
//			jrstaticText.setBottomBorder(JRBaseLine.PEN_1_POINT);  
			jrstaticText.setForecolor(Color.blue);
		//	jrstaticText.setStretchType((byte)0);
			band.addElement(jrstaticText);  
			
			// 定義欄位,註冊欄位
			JRDesignField field = new JRDesignField();
			field.setName(alias[i]);
			field.setValueClass(String.class);
			jasperDesign.addField(field);
			
			// add text fields for displaying fields
			JRDesignTextField textField = new JRDesignTextField();
			JRDesignExpression expression = new JRDesignExpression();
			expression.setText("$F{" + alias[i] + "}");
			expression.setValueClass(String.class);
			textField.setExpression(expression);
			textField.setFontSize(fontSize);
			textField.setHeight(textHeight);
			textField.setWidth(textWidth);
			textField.setX(X * i);
			textField.setPdfFontName("STSong-Light"); 
			textField.setPdfEmbedded(true);
			textField.setPdfEncoding("UniGB-UCS2-H");  
			textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
			textField.setLeftBorder(JRBaseLine.PEN_1_POINT);
			textField.setTopBorder(JRBaseLine.PEN_1_POINT);
			textField.setRightBorder(JRBaseLine.PEN_1_POINT);
			textField.setBottomBorder(JRBaseLine.PEN_1_POINT);
			//textField.setForecolor(new Color(0x99,0xFF,0xFF));
			textField.setForecolor(Color.blue);
			textField.setStretchWithOverflow(true);
			detail.addElement(textField);
		}
		 
		jasperDesign.setColumnHeader(band);
		
		//detail
		jasperDesign.setDetail(detail);
		
		// Column footer
		band = new JRDesignBand();
		band.setHeight(10);
		jasperDesign.setColumnFooter(band);
		// Page footer
		band = new JRDesignBand();
		band.setHeight(80);
		
		staticText = new JRDesignStaticText();
		staticText.setX(0); 
		staticText.setY(0);
		staticText.setWidth(100);
		staticText.setHeight(20);
		staticText.setPdfFontName("STSong-Light");   
		staticText.setPdfEmbedded(true);   
		staticText.setPdfEncoding("UniGB-UCS2-H");  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		staticText.setText(sdf.format(new Date()));
		band.addElement(staticText);
		
		
		JRDesignTextField textField = new JRDesignTextField();
		textField.setX(450); 
		textField.setY(0);
		textField.setWidth(100);
		textField.setHeight(20);
		textField.setPdfFontName("STSong-Light");   
		textField.setPdfEmbedded(true);   
		textField.setPdfEncoding("UniGB-UCS2-H");  
		
		JRDesignExpression expression = new JRDesignExpression();
		 expression = new JRDesignExpression();
		 expression.setValueClass(java.lang.Integer.class);		
		 expression.setText("$V{PAGE_NUMBER}");
		 
		textField.setExpression(expression);
		band.addElement(textField);
		jasperDesign.setPageFooter(band);
		
		System.out.println(expression.getText());
		// Summary
		band = new JRDesignBand();
		band.setHeight(0);

		/* **************************************************************** */
		/* Here My doubt */
		/*
		 * JRDesignChart chart1 = new
		 * JRDesignChart(null,JRChart.CHART_TYPE_LINE); chart1.setHeight(100);
		 * chart1.setBorderColor(Color.red); chart1.setForecolor(Color.BLACK);
		 * chart1.setWidth(200);
		 */

		// band.addElement(chart1);
		jasperDesign.setSummary(band);

		System.out.println("After Connection");

		JasperReport jasperReport = JasperCompileManager
				.compileReport(jasperDesign);
		Map parameters = new HashMap();
		System.out.println("After Parameter");
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
				parameters, connection);
		System.out.println("After Print");
		
		JasperExportManager.exportReportToHtmlFile(jasperPrint, "D://test2.html");
		JasperExportManager.exportReportToPdfFile(jasperPrint, "D://test2.pdf");
		JasperExportManager.exportReportToXmlFile(jasperPrint, "D://test2.xml", false);
		
		System.out.println("After Export");

	}

	private void addReportFiled(JasperDesign jasperDesign) throws JRException {
		JRDesignField field;
		field=new JRDesignField();
		field.setName("USERID");
		field.setValueClass(java.math.BigDecimal.class);
		jasperDesign.addField(field);
		
		field = new JRDesignField();
		field.setName("NAME");
		field.setValueClass(java.lang.String.class);
		jasperDesign.addField(field);
		
		field = new JRDesignField();
		field.setName("AGE");
		field.setValueClass(java.lang.String.class);
		jasperDesign.addField(field);
		
		field = new JRDesignField();
		field.setName("SEX");
		field.setValueClass(java.lang.String.class);
		jasperDesign.addField(field);
		
		field = new JRDesignField();
		field.setName("PASSWORD");
		field.setValueClass(java.lang.String.class);
		jasperDesign.addField(field);
		
		field = new JRDesignField();
		field.setName("DEPARTMENT");
		field.setValueClass(java.lang.String.class);
		jasperDesign.addField(field);
	}

	private JRDesignStyle setReportSytle(JasperDesign jasperDesign)
			throws JRException {
		jasperDesign.setName("SampleReport");
		jasperDesign.setPageWidth(595);
		jasperDesign.setPageHeight(842);
		jasperDesign.setColumnWidth(535);
		jasperDesign.setColumnSpacing(0);
		
		jasperDesign.setLeftMargin(30);
		jasperDesign.setRightMargin(30);
		jasperDesign.setTopMargin(20);
		jasperDesign.setBottomMargin(20);
		// whenNoDataType="NoPages"
		jasperDesign.setWhenNoDataType(JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE);
		// isTitleNewPage="false"
		jasperDesign.setTitleNewPage(false);
		// isSummaryNewPage="false"
		jasperDesign.setSummaryNewPage(false);
	//	jasperDesign.setOrientation(JRReport.ORIENTATION_PORTRAIT);
	//	jasperDesign.setPrintOrder(JRReport.PRINT_ORDER_VERTICAL);
		
		JRDesignStyle normalStyle = new JRDesignStyle();
		normalStyle.setName("Arial_Normal");
		normalStyle.setDefault(true);
		normalStyle.setFontName("Arial");
		normalStyle.setFontSize(12);
		normalStyle.setPdfFontName("STSong-Light");
		normalStyle.setPdfEncoding("UniGB-UCS2-H");
		normalStyle.setPdfEmbedded(true);
		jasperDesign.addStyle(normalStyle);

		JRDesignStyle boldStyle = new JRDesignStyle();
		boldStyle.setName("Arial_Bold");
		boldStyle.setFontName("Arial");
		boldStyle.setFontSize(12);
		boldStyle.setBold(true);
		boldStyle.setPdfFontName("STSong-Light");
		boldStyle.setPdfEncoding("UniGB-UCS2-H");
		boldStyle.setPdfEmbedded(true);
		jasperDesign.addStyle(boldStyle);

		JRDesignStyle italicStyle = new JRDesignStyle();
		italicStyle.setName("Arial_Italic");
		italicStyle.setFontName("Arial");
		italicStyle.setFontSize(12);
		italicStyle.setItalic(true);
		italicStyle.setPdfFontName("STSong-Light");
		italicStyle.setPdfEncoding("UniGB-UCS2-H");
		italicStyle.setPdfEmbedded(true);
		jasperDesign.addStyle(italicStyle);
		return normalStyle;
	}

	private Connection getConnection() throws SQLException {
		Connection connection = ReadConfig.getProduceInstance().getDataSource()
				.getConnection();
		return connection;
	}

}

	class Sample {
	public static void main(String args[]) throws Exception {
		Sample6 obj = new Sample6();

	}

}

 然後是豎排的模板

Java程式碼 複製程式碼
  1. package  cn.hzmc.dynamicreport.example8;   
  2. import  java.awt.Color;   
  3. import  java.sql.Connection;   
  4. import  java.sql.SQLException;   
  5. import  java.text.SimpleDateFormat;   
  6. import  java.util.Date;   
  7. import  java.util.HashMap;   
  8. import  java.util.Map;   
  9. import  net.sf.jasperreports.engine.JRAlignment;   
  10. import  net.sf.jasperreports.engine.JRException;   
  11. import  net.sf.jasperreports.engine.JRReport;   
  12. import  net.sf.jasperreports.engine.JasperCompileManager;   
  13. import  net.sf.jasperreports.engine.JasperExportManager;   
  14. import  net.sf.jasperreports.engine.JasperFillManager;   
  15. import  net.sf.jasperreports.engine.JasperPrint;   
  16. import  net.sf.jasperreports.engine.JasperReport;   
  17. import  net.sf.jasperreports.engine.base.JRBaseLineBox;   
  18. import  net.sf.jasperreports.engine.base.JRBasePrintText;   
  19. import  net.sf.jasperreports.engine.design.JRDesignBand;   
  20. import  net.sf.jasperreports.engine.design.JRDesignExpression;   
  21. import  net.sf.jasperreports.engine.design.JRDesignField;   
  22. import  net.sf.jasperreports.engine.design.JRDesignLine;   
  23. import  net.sf.jasperreports.engine.design.JRDesignQuery;   
  24. import  net.sf.jasperreports.engine.design.JRDesignStaticText;   
  25. import  net.sf.jasperreports.engine.design.JRDesignStyle;   
  26. import  net.sf.jasperreports.engine.design.JRDesignTextField;   
  27. import  net.sf.jasperreports.engine.design.JasperDesign;   
  28. import  com.capaa.pipe.config.ReadConfig;   
  29. class  Sample {   
  30. @SuppressWarnings ( "deprecation" )   
  31.     Sample()  throws  Exception {   
  32.         Connection connection = getConnection();   
  33.         JasperDesign jasperDesign =  new  JasperDesign();   
  34.         JRDesignStyle normalStyle = setReportSytle(jasperDesign);   
  35.         JRDesignQuery query =  new  JRDesignQuery();   
  36.         query.setText( "select userid,name,age,sex,password,department from person" );   
  37.         jasperDesign.setQuery(query);          
  38. //addReportFiled(jasperDesign);
  39. // Title
  40.         JRDesignBand band =  new  JRDesignBand();   
  41.         band.setHeight( 20 );   
  42.         JRDesignStaticText staticText =  new  JRDesignStaticText();   
  43.         staticText.setX( 200 );   
  44.         staticText.setY( 0 );   
  45.         staticText.setWidth( 200 );   
  46.         staticText.setHeight( 15 );   
  47.         staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);   //設定文字的對齊方式
  48.         staticText.setStyle(normalStyle);   
  49.         staticText.setText( "杭州美創科技有限公司審計報表 " );   
  50.         band.addElement(staticText);           
  51.         JRDesignLine line =  new  JRDesignLine();        
  52.         line.setX( 0 );   
  53.         line.setY( 19 );   
  54.         line.setWidth( 500 );   
  55.         line.setHeight( 1 );   
  56.         line.setForecolor( new  Color( 0x99 , 0xFF , 0xFF ));   
  57.         band.addElement(line);   
  58.         jasperDesign.setTitle(band);   
  59. // Page header
  60.         band =  new  JRDesignBand();   
  61. //band.setHeight(20);
  62.         band.setHeight( 0 );   
  63.         jasperDesign.setPageHeader(band);   
  64. // Column header
  65.         band =  new  JRDesignBand();   
  66.         band.setHeight( 0 );   
  67.         jasperDesign.setColumnHeader(band);   
  68. //detail
  69. // 開始新增列欄位
  70.         String[] headers = { "USERID" , "NAME" , "AGE" , "SEX" , "PASSWORD" , "DEPARTMENT" };   
  71.         String[] alias = { "USERID" , "NAME" , "AGE" , "SEX" , "PASSWORD" , "DEPARTMENT" };   
  72. int  Y =  20 ;   
  73. int  columnHeaderfontSize =  10 ;   
  74. int  fontSize =  8 ;   
  75. int  textHeight =  19 ;   
  76. int  textWidth =  180 ;   
  77. int  detailHeight =  130 ;   
  78.          JRDesignBand detail =  new  JRDesignBand();   
  79.          detail.setHeight(detailHeight);   
  80. for ( int  i= 0 ;i<headers.length;i++){   
  81.          }   
  82. for ( int  i =  0 ;i<headers.length;i++){   
  83.             JRDesignStaticText jrstaticText =  new  JRDesignStaticText();     
  84.             jrstaticText.setText(headers[i]);    
  85.             jrstaticText.setFontSize(columnHeaderfontSize);     
  86.             jrstaticText.setHeight(textHeight);     
  87.             jrstaticText.setWidth(textWidth);    
  88.             jrstaticText.setY(Y * i);     
  89.             jrstaticText.setPdfFontName( "STSong-Light" );     
  90.             jrstaticText.setPdfEmbedded( true );     
  91.             jrstaticText.setPdfEncoding( "UniGB-UCS2-H" );     
  92. //  jrstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);  
  93.             jrstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);     
  94. /*jrstaticText.setLeftBorder(JRBaseLine.PEN_1_POINT);    
  95.             jrstaticText.setTopBorder(JRBaseLine.PEN_1_POINT);    
  96.             jrstaticText.setRightBorder(JRBaseLine.PEN_1_POINT);    
  97.             jrstaticText.setBottomBorder(JRBaseLine.PEN_1_POINT); */
  98.             jrstaticText.setForecolor(Color.black);   
  99. //  jrstaticText.setStretchType((byte)0);
  100. //  band.addElement(jrstaticText);  
  101.             detail.addElement(jrstaticText);   
  102. // 定義欄位,註冊欄位
  103.             JRDesignField field =  new  JRDesignField();   
  104.             field.setName(alias[i]);   
  105.             field.setValueClass(String. class );   
  106.             jasperDesign.addField(field);   
  107. // add text fields for displaying fields
  108.             JRDesignTextField textField =  new  JRDesignTextField();   
  109.             JRDesignExpression expression =  new  JRDesignExpression();   
  110.             expression.setText( "$F{"  + alias[i] +  "}" );   
  111.             expression.setValueClass(String. class );   
  112.             textField.setExpression(expression);   
  113.             textField.setFontSize(fontSize);   
  114.             textField.setHeight(textHeight);   
  115.             textField.setWidth(textWidth);   
  116.             textField.setX(textWidth);   
  117.             textField.setY(Y*i);   
  118.             textField.setPdfFontName( "STSong-Light" );    
  119.             textField.setPdfEmbedded( true );   
  120.             textField.setPdfEncoding( "UniGB-UCS2-H" );     
  121. //textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
  122.             textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);   
  123. /*textField.setLeftBorder(JRBaseLine.PEN_1_POINT);  
  124.             textField.setTopBorder(JRBaseLine.PEN_1_POINT);  
  125.             textField.setRightBorder(JRBaseLine.PEN_1_POINT);  
  126.             textField.setBottomBorder(JRBaseLine.PEN_1_POINT);*/
  127. /*JRBaseLineBox baseLineBox = new JRBaseLineBox(textField);  
  128.             baseLineBox.getLeftPen().setLineColor(Color.black);  
  129.             baseLineBox.getLeftPen().setLineWidth(0);  
  130.             baseLineBox.getLeftPen().setLineStyle((byte)0);  
  131.             baseLineBox.getRightPen().setLineColor(Color.black);  
  132.             baseLineBox.getRightPen().setLineWidth(0);  
  133.             baseLineBox.getRightPen().setLineStyle((byte)0);  
  134.             baseLineBox.getTopPen().setLineColor(Color.black);  
  135.             baseLineBox.getTopPen().setLineWidth(0);  
  136.             baseLineBox.getTopPen().setLineStyle((byte)0);  
  137.             baseLineBox.getBottomPen().setLineColor(Color.black);  
  138.             baseLineBox.getBottomPen().setLineWidth(0);  
  139.             baseLineBox.getBottomPen().setLineStyle((byte)0);*/
  140. //textField.setForecolor(new Color(0x99,0xFF,0xFF));
  141.             textField.setForecolor(Color.black);   
  142.             textField.setStretchWithOverflow( true );   
  143.             detail.addElement(textField);   
  144.         }   
  145. //  jasperDesign.setColumnHeader(band);
  146. //detail
  147.         jasperDesign.setDetail(detail);   
  148. // Column footer
  149.         band =  new  JRDesignBand();   
  150.         band.setHeight( 0 );   
  151.         jasperDesign.setColumnFooter(band);   
  152. // Page footer
  153.         band =  new  JRDesignBand();   
  154.         band.setHeight( 20 );   
  155.         JRDesignLine line2 =  new  JRDesignLine();           
  156.         line2.setX( 0 );   
  157.         line2.setY(- 10 );   
  158.         line2.setWidth( 450 );   
  159.         line2.setHeight( 0 );   
  160.         line2.setForecolor( new  Color( 0x99 , 0xFF , 0xFF ));   
  161.         band.addElement(line2);   
  162.         staticText =  new  JRDesignStaticText();   
  163.         staticText.setX( 0 );    
  164.         staticText.setY( 0 );   
  165.         staticText.setWidth( 100 );   
  166.         staticText.setHeight( 20 );   
  167.         staticText.setPdfFontName( "STSong-Light" );      
  168.         staticText.setPdfEmbedded( true );      
  169.         staticText.setPdfEncoding( "UniGB-UCS2-H" );     
  170.         SimpleDateFormat sdf =  new  SimpleDateFormat( "yyyy/MM/dd" );   
  171.         staticText.setText(sdf.format( new  Date()));   
  172.         band.addElement(staticText);   
  173.         JRDesignTextField textField =  new  JRDesignTextField();   
  174.         textField.setX( 450 );    
  175.         textField.setY( 0 );   
  176.         textField.setWidth( 100 );   
  177.         textField.setHeight( 20 );   
  178.         textField.setPdfFontName( "STSong-Light" );      
  179.         textField.setPdfEmbedded( true );      
  180.         textField.setPdfEncoding( "UniGB-UCS2-H" );     
  181.         JRDesignExpression expression =  new  JRDesignExpression();   
  182.          expression =  new  JRDesignExpression();   
  183.          expression.setValueClass(java.lang.Integer. class );        
  184.          expression.setText( "$V{PAGE_NUMBER}" );   
  185.         textField.setExpression(expression);   
  186.         band.addElement(textField);   
  187.         jasperDesign.setPageFooter(band);   
  188.         System.out.println(expression.getText());   
  189. // Summary
  190.         band =  new  JRDesignBand();   
  191.         band.setHeight( 0 );   
  192.         jasperDesign.setSummary(band);   
  193.         System.out.println( "After Connection" );   
  194.         JasperReport jasperReport = Jasp