JasperReport JavaBean作为数据源简单例子

1.所需jar包

 ireport的安装目录下:

iReport-3.7.3ireportmodulesext(如果为3.7.3):

jasperreports-3.7.3.jar
iText-2.1.7.jar
groovy-all-1.5.5.jar
commons-digester-1.7.jar
commons-collections-3.2.1.jar
commons-beanutils-1.8.2.jar

2.中文问题

需要iText-verson.jar和iTextAsia.jar

①设置字体为宋体

②pdf embedded 勾上

③设置 pdf encoding 为 UniGB-UCS2-H

④设置Pdf font name is now .. 为 STSong-Light

 

3.如果使用javabean为数据源,则要写一个继承JRDataSource的数据源类,如

public class RepairContactMenuDataSource implements JRDataSource {
        
    public RepairContactMenuDataSource(List datas) {
        this.datas = datas;
    }

    private List datas = null ;
    private int index = -1 ;
    
    public Object getFieldValue(JRField field) throws JRException {
     RepairContactMenu repairContactMenu = (RepairContactMenu)this.datas.get(index);
    
     Object value = null;
     String fieldName = field.getName();  
       if ("serialNum".equals(fieldName)) {  
           if(repairContactMenu.getSerialNum()!=null){
               value = repairContactMenu.getSerialNum();
           } else{
               value = "";
           }
       } else if ("date".equals(fieldName)){  
           if(repairContactMenu.getDate()!=null){
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
               value = sdf.format(repairContactMenu.getDate());
           } else {
               value = "";
           }
       } else if("reason".equals(fieldName)){
           if(repairContactMenu.getReason()!=null){
               value = repairContactMenu.getReason();
           } else {
               value = "";
           }
       }        
       repairContactMenu = null ; 
       return value;
    }

    public boolean next() throws JRException {
        index ++;
        return index < datas.size();
    }

}
4.可以写一个测试类,如下

public class RepairContactMenuDataSourceProvider extends JRAbstractBeanDataSourceProvider {

    public RepairContactMenuDataSourceProvider(Class beanClass) {
        super(beanClass);
    }

    @Override
    public JRDataSource create(JasperReport arg0) throws JRException {
        List list = new ArrayList();
        RepairContactMenu repairContactMenu =  new RepairContactMenu();
        repairContactMenu.setSerialNum("2010");
        list.add(repairContactMenu);
        return new JRBeanCollectionDataSource(list);
    }

    @Override
    public void dispose(JRDataSource arg0) throws JRException {

    }
   
    public static void main(String[] args) throws JRException{
        List list = new ArrayList();
        RepairContactMenu repair = new RepairContactMenu();
        repair.setSerialNum("4444");
        repair.setDate(new Date());
        repair.setContent("dfdfdf");
        repair.setDepartmentView("dfdfdf");
        repair.setReason("是由");
          
        list.add(repair);
        String filename = "F:\*\report2.jasper";
        JasperPrint print = JasperFillManager.fillReport(filename, null, new RepairContactMenuDataSource(list));
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,    "F:\*.pdf");
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exporter.exportReport();
    }

}

4.Servlet类

使用了spring的mvc

@Controller
public class ExportReportContactMenuServlet {
    @Autowired
    private RepairContactMenuService repairContactMenuService;
   
    @RequestMapping("/excel/exportToPdf.do")
     protected void export(HttpServletRequest request, HttpServletResponse response)
        throws IOException, BiffException, WriteException, JRException {        
        String repairContactMenuId = request.getParameter("id");
        RepairContactMenu repairContactMenu = (RepairContactMenu)repairContactMenuService.loadById(repairContactMenuId);
        response.setContentType("application/pdf");
        response.setCharacterEncoding("UTF-8");
        String jrFilePath = request.getSession().getServletContext().getRealPath("/report/report2.jasper");
        JasperReport report = (JasperReport)JRLoader.loadObject(jrFilePath);
        Map parameters = new HashMap();
        String imagePath = request.getSession().getServletContext().getRealPath("/report/isAudited.gif");
        parameters.put("image_uri", imagePath);
        parameters.put("isAudited", isAudited);
        List repairContactMenus = new ArrayList();
        repairContactMenus.add(repairContactMenu);
        byte[] bytes = JasperRunManager.runReportToPdf(report, parameters, new RepairContactMenuDataSource(repairContactMenus));
        response.setContentLength(bytes.length);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes, 0, bytes.length);
        outputStream.flush();
        outputStream.close();
    }
}

 

5.JasperReport基本概念

 字段,参数和变量:Fields,Parameters,Variable;

 报表结构

 Title Band:在整个报表的第一页的最上面部分显示,其他页面不会出现此band内容;

 pageHeader Band:其内容会在整个报表中的每一个页面中出现,显示在页面的上部,如果是报表的第一页其内容将显示在Title Band下面,除首页之外的所有页面中pageHeader内容将显示在页面的最上端;
 pageFooter Band:显示在所有页面的最下端;
 Detail Band:报表内容段,是报表设计中需要重复出现的内容,在每页都会出现;9 c- q4 E: V" p. w4 g1 R6 k. ~
 columnHeader Band:针对Detail Band的表头段,一般情况下在这个段中画报表的表头;$ g  W* H' ^' t/ m* Z/ a
 columnFooter Band:针对Detail Band的表尾段;9 b9 N7 {) y! H. P( Z
 Summary Band:表格的合计段,出现在整个报表的最后一页中的Detail band的后面,一般用来统计报表中的某一个或某几个 

 

http://blog.sina.com.cn/s/blog_8020e41101015p4c.html

你可能感兴趣的:(jasperReport)