jasperreports是一个用来开发报表的组件,通常和ireport来配合使用,可以快速的构建出漂亮的报表。但是ireport画出的报表模板过于死板,就是不支持动态的列。现在要自己实现一个报表模板,先参考ireport工具画出的模板,然后改改,用java代码自己实现一个。
项目组目前就是这么做的,自己去写一个模板,一个横排的,一个竖排的,应为字段比较多,一般采用竖排的。我们项目组做动态报表没有用DynamicJasper这个开源的动态报表的组件,原因是我觉得他用起来也不太方便。还有就是时间的问题。
做了两个模板,现在备份下来。首先是横排的模板
- 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);
-
-
-
- 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);
-
-
- band = new JRDesignBand();
-
- band.setHeight( 0 );
- jasperDesign.setPageHeader(band);
-
-
- 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.setForecolor(Color.blue);
-
- band.addElement(jrstaticText);
-
-
- JRDesignField field = new JRDesignField();
- field.setName(alias[i]);
- field.setValueClass(String. class );
- jasperDesign.addField(field);
-
-
- 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(Color.blue);
- textField.setStretchWithOverflow( true );
- detail.addElement(textField);
- }
-
- jasperDesign.setColumnHeader(band);
-
-
- jasperDesign.setDetail(detail);
-
-
- band = new JRDesignBand();
- band.setHeight( 10 );
- jasperDesign.setColumnFooter(band);
-
- 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());
-
- band = new JRDesignBand();
- band.setHeight( 0 );
-
-
-
-
-
-
-
-
-
-
-
- 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 );
-
- jasperDesign.setWhenNoDataType(JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE);
-
- jasperDesign.setTitleNewPage( false );
-
- jasperDesign.setSummaryNewPage( false );
-
-
-
- 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();
-
- }
-
- }
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();
}
}
然后是竖排的模板
- package cn.hzmc.dynamicreport.example8;
-
- 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.JRBaseLineBox;
- 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 Sample {
- @SuppressWarnings ( "deprecation" )
- Sample() 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);
-
-
-
- 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);
-
-
- band = new JRDesignBand();
-
- band.setHeight( 0 );
- jasperDesign.setPageHeader(band);
-
-
- band = new JRDesignBand();
- band.setHeight( 0 );
- jasperDesign.setColumnHeader(band);
-
-
-
- String[] headers = { "USERID" , "NAME" , "AGE" , "SEX" , "PASSWORD" , "DEPARTMENT" };
- String[] alias = { "USERID" , "NAME" , "AGE" , "SEX" , "PASSWORD" , "DEPARTMENT" };
- int Y = 20 ;
- int columnHeaderfontSize = 10 ;
- int fontSize = 8 ;
- int textHeight = 19 ;
- int textWidth = 180 ;
- int detailHeight = 130 ;
- JRDesignBand detail = new JRDesignBand();
- detail.setHeight(detailHeight);
- for ( int i= 0 ;i<headers.length;i++){
-
- }
- 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);
-
- jrstaticText.setY(Y * i);
- jrstaticText.setPdfFontName( "STSong-Light" );
- jrstaticText.setPdfEmbedded( true );
- jrstaticText.setPdfEncoding( "UniGB-UCS2-H" );
-
- jrstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);
-
-
-
-
- jrstaticText.setForecolor(Color.black);
-
-
- detail.addElement(jrstaticText);
-
-
- JRDesignField field = new JRDesignField();
- field.setName(alias[i]);
- field.setValueClass(String. class );
- jasperDesign.addField(field);
-
-
- 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(textWidth);
- textField.setY(Y*i);
- textField.setPdfFontName( "STSong-Light" );
- textField.setPdfEmbedded( true );
- textField.setPdfEncoding( "UniGB-UCS2-H" );
-
- textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- textField.setForecolor(Color.black);
- textField.setStretchWithOverflow( true );
- detail.addElement(textField);
- }
-
-
-
-
- jasperDesign.setDetail(detail);
-
-
- band = new JRDesignBand();
- band.setHeight( 0 );
- jasperDesign.setColumnFooter(band);
-
- band = new JRDesignBand();
- band.setHeight( 20 );
-
- JRDesignLine line2 = new JRDesignLine();
- line2.setX( 0 );
- line2.setY(- 10 );
- line2.setWidth( 450 );
- line2.setHeight( 0 );
- line2.setForecolor( new Color( 0x99 , 0xFF , 0xFF ));
- band.addElement(line2);
-
- 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());
-
- band = new JRDesignBand();
- band.setHeight( 0 );
-
- 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://test3.html" );
- JasperExportManager.exportReportToPdfFile(jasperPrint, "D://test3.pdf" );
- JasperExportManager.exportReportToXmlFile(jasperPrint, "D://test3.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( 335 );
- jasperDesign.setColumnSpacing( 0 );
-
- jasperDesign.setLeftMargin( 30 );
- jasperDesign.setRightMargin( 30 );
- jasperDesign.setTopMargin( 20 );
- jasperDesign.setBottomMargin( 20 );
-
- jasperDesign.setWhenNoDataType(JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE);
-
- jasperDesign.setTitleNewPage( false );
-
- jasperDesign.setSummaryNewPage( false );
-
-
-
- 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 Vertical {
- public static void main(String args[]) throws Exception {
- Sample obj = new Sample();
-
- }
-
- }
package cn.hzmc.dynamicreport.example8;
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.JRBaseLineBox;
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 Sample {
@SuppressWarnings("deprecation")
Sample() 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(0);
jasperDesign.setColumnHeader(band);
//detail
// 开始添加列字段
String[] headers = {"USERID","NAME","AGE","SEX","PASSWORD","DEPARTMENT"};
String[] alias = {"USERID","NAME","AGE","SEX","PASSWORD","DEPARTMENT"};
int Y = 20;
int columnHeaderfontSize = 10;
int fontSize = 8;
int textHeight = 19;
int textWidth = 180;
int detailHeight = 130;
JRDesignBand detail = new JRDesignBand();
detail.setHeight(detailHeight);
for(int i=0;i<headers.length;i++){
}
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);
jrstaticText.setY(Y * i);
jrstaticText.setPdfFontName("STSong-Light");
jrstaticText.setPdfEmbedded(true);
jrstaticText.setPdfEncoding("UniGB-UCS2-H");
// jrstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
jrstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);
/*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.black);
// jrstaticText.setStretchType((byte)0);
// band.addElement(jrstaticText);
detail.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(textWidth);
textField.setY(Y*i);
textField.setPdfFontName("STSong-Light");
textField.setPdfEmbedded(true);
textField.setPdfEncoding("UniGB-UCS2-H");
//textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
/*textField.setLeftBorder(JRBaseLine.PEN_1_POINT);
textField.setTopBorder(JRBaseLine.PEN_1_POINT);
textField.setRightBorder(JRBaseLine.PEN_1_POINT);
textField.setBottomBorder(JRBaseLine.PEN_1_POINT);*/
/*JRBaseLineBox baseLineBox = new JRBaseLineBox(textField);
baseLineBox.getLeftPen().setLineColor(Color.black);
baseLineBox.getLeftPen().setLineWidth(0);
baseLineBox.getLeftPen().setLineStyle((byte)0);
baseLineBox.getRightPen().setLineColor(Color.black);
baseLineBox.getRightPen().setLineWidth(0);
baseLineBox.getRightPen().setLineStyle((byte)0);
baseLineBox.getTopPen().setLineColor(Color.black);
baseLineBox.getTopPen().setLineWidth(0);
baseLineBox.getTopPen().setLineStyle((byte)0);
baseLineBox.getBottomPen().setLineColor(Color.black);
baseLineBox.getBottomPen().setLineWidth(0);
baseLineBox.getBottomPen().setLineStyle((byte)0);*/
//textField.setForecolor(new Color(0x99,0xFF,0xFF));
textField.setForecolor(Color.black);
textField.setStretchWithOverflow(true);
detail.addElement(textField);
}
// jasperDesign.setColumnHeader(band);
//detail
jasperDesign.setDetail(detail);
// Column footer
band = new JRDesignBand();
band.setHeight(0);
jasperDesign.setColumnFooter(band);
// Page footer
band = new JRDesignBand();
band.setHeight(20);
JRDesignLine line2 = new JRDesignLine();
line2.setX(0);
line2.setY(-10);
line2.setWidth(450);
line2.setHeight(0);
line2.setForecolor(new Color(0x99,0xFF,0xFF));
band.addElement(line2);
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);
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://test3.html");
JasperExportManager.exportReportToPdfFile(jasperPrint, "D://test3.pdf");
JasperExportManager.exportReportToXmlFile(jasperPrint, "D://test3.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(335);
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 Vertical {
public static void main(String args[]) throws Exception {
Sample obj = new Sample();
}
}