`
coconut_zhang
  • 浏览: 544278 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论
阅读更多

Jasperreport的文档表明:
    1> 开发静态报表建议采纳模板方式
    2> 开发动态报表采用直接编辑JasperDesign对象。
但是似乎jasperreport对动态报表的操作缺乏高效率的工具类,因而开发
起来特别的费劲。

经过多方研究,得到了2种解决方案:
1> 结合Velocity技术动态生成jrxml模板文件;
2> 采用第三方工具DynamicJasper:DynamicJasper是sourceforge上的一个
开源项目,把它与jasperreport集成,可以相对轻松的解决大多数动态报表问题,
并且为进一步开发用户自定义报表提供了有效途径。
我采用的是第二种方案。

 

 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);  
  //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);  
  //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();

 }

}

 

 

分享到:
评论

相关推荐

    使用jasperreports制作报表(导出pdf excel html)

    **使用JasperReports制作报表(导出PDF, Excel, HTML)** JasperReports是一款开源的Java报表库,它允许开发者...通过Java代码与报表模板的结合,可以方便地在应用程序中动态生成和导出报表,满足各种格式和数据需求。

    JasperReports开源报表概述和运行原理.doc

    然后,JasperReports引擎会根据这些模板和数据源动态生成报表,支持多种输出格式,如PDF、HTML、Excel、CSV等。 总的来说,JasperReports是一个强大且灵活的报表解决方案,广泛应用于各种Java应用中,为开发者提供...

    iReport+jasperReports JAVA报表

    iReport和JasperReports是Java世界中非常流行的报表设计和生成工具,它们为开发者提供了强大的报表解决方案。在本文中,我们将深入探讨这两个工具的核心特性、使用场景以及如何联合使用它们来创建复杂的JAVA报表。 ...

    在Struts2中使用JasperReports生成报表

    将Struts2与JasperReports结合使用,可以方便地在Web应用中实现动态报表的生成和展示。 首先,我们需要了解Struts2中整合JasperReports的基本步骤: 1. **环境准备**:确保你已经安装了MyEclipse开发环境,并且在...

    jasperreports 生成报表

    JasperReports 是一款开源的报表工具,用于设计和生成各种类型的报表,包括 PDF、Excel、XML 等格式。在使用 JasperReports 生成报表时,你需要遵循以下详细流程: 1. **新建报表**: - 打开 JasperReports 设计...

    JasperReports 报表开发 java.rar

    7. **动态报表**:动态报表是JasperReports的一大特色,它允许在运行时改变报表的结构和内容,例如根据用户输入条件动态生成报表,或者在报表中加入实时数据。 通过阅读“JasperReports iRepor 报表开发详解.pdf”...

    iReport-JasperReports报表开发指南+操作手册

    《iReport-JasperReports报表开发指南+操作手册》是一份详尽的资源,旨在帮助开发者深入理解和高效使用iReport和JasperReports这两个强大的报表工具。iReport是JasperReports的图形化设计工具,用于创建复杂的报表...

    JasperReports数据库查询父子报表模板

    JasperReports是一款强大的开源报表工具,广泛应用于Java环境中,用于生成静态或动态的报告。在这个主题中,我们主要探讨如何使用JasperReports实现数据库查询并创建父子报表模板。 1. **JasperReports基础** ...

    jaspersoft-studio-user-guide_8_jasperReports_报表设计器_

    **JasperReports报表设计器——Jaspersoft Studio用户指南** JasperReports是一款强大的开源报表工具,其报表设计器——Jaspersoft Studio,是专为简化和优化报表创建而设计的集成开发环境(IDE)。这款强大的工具...

    ireport+jasperreports的报表

    ### iReport与JasperReports报表开发详解 #### 一、简介 JasperReports是一个功能强大的报表制作工具,它允许用户根据特定的规则编写XML文件,从而生成多种格式的输出文件,例如PDF、HTML、XML、XLS等。iReport则是...

    struts2基于jasperreports多报表同时打印插件

    基于struts2-jasperreports-plugin-2.3.1.2改写的插件,在原有功能的基础上又添加了多报表同时打印的功能。建议没用过struts2-jasperreports插件的先学习怎么使用这个插件。配的文档只对添加的功能的使用方法做了...

    jasperreports(ireport1.2.5)制作动态PDF报表

    具体模版*.jrxml 博文链接:https://xiaobo.iteye.com/blog/34774

    动态生成JasperReports报表

    该小项目以编码形式实现了JaperReports的动态报表功能,以满足用户对报表的高度定制化。 使用该项目时的注意事项: 1.使用Eclipse导入该项目 2.下载JasperReports Library,将JasperReports的jar文件及其依赖jar...

    JasperReports,iReport制作报表

    你可以传递参数来动态调整报表内容。 5. **展示报表**:JasperExportManager或JasperViewer类可以用于导出或显示报表,支持多种格式,如PDF、HTML、Excel、CSV等。在Web环境中,通常会将报表导出为PDF或HTML,然后...

    Struts2整合jasperReports报表

    iReport是用于设计jasperReports报表的图形化界面,允许开发者使用拖拽方式创建和编辑报表模板。 以下是整合Struts2和jasperReports的步骤: 1. **引入依赖**:在项目的pom.xml或build.gradle文件中添加...

    iReport+jasperReports 联合开发JAVA报表

    1. **iReport**:这是一个基于Java的报表设计工具,它允许开发者通过直观的用户界面创建、编辑和管理JasperReports报表模板。iReport支持多种数据源,包括数据库、CSV、XML等,并提供丰富的图表类型和样式选项。 2....

    jasperreports打印word格式报表的问题

    标题 "jasperreports打印word格式报表的问题" 涉及的是使用JasperReports库生成并打印Word(.doc或.docx)格式报表时遇到的技术挑战。JasperReports是一款开源Java库,它允许开发者创建、设计和导出各种类型的报告,...

Global site tag (gtag.js) - Google Analytics