iReport5.5.1+jasperreports5.5.1 制作报表

iReport5.5.1+jasperreports5.5.1 制作报表一、核心Jar包itext-1.3.1.jariTextAsian.jaritextpdf-5.5.11.jarjasperreports-0.5.3.jarcommons-beanutils.jarcommons-collections.jarcommons-digester.jarcommons-logging.jargroovy-all-2.4.4.jar…

大家好,欢迎来到IT知识分享网。

一、核心Jar包

itext-1.3.1.jar

iTextAsian.jar

itextpdf-5.5.11.jar

jasperreports-0.5.3.jar

commons-beanutils.jar

commons-collections.jar

commons-digester.jar

commons-logging.jar

groovy-all-2.4.4.jar

servlet-api.jar

项目结构:

iReport5.5.1+jasperreports5.5.1 制作报表

 

二、使用iReport5.5.1制作报表模板

1、面板组件

iReport5.5.1+jasperreports5.5.1 制作报表

iReport5.5.1+jasperreports5.5.1 制作报表

iReport5.5.1+jasperreports5.5.1 制作报表

iReport5.5.1+jasperreports5.5.1 制作报表

iReport5.5.1+jasperreports5.5.1 制作报表

 

三、测试代码(这里写的是一个Servlet)

package demo.ireport.servlet;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
import demo.ireport.vo.People;

@WebServlet("/jasperPdfTest")
public class jasperPdfTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public jasperPdfTest() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("param", "我是传入的参数");
		
		ArrayList<People> list = new ArrayList<People>();
		People people1 = new People(1,"张三",20);
		People people2 = new People(2,"李四",20);
		People people3 = new People(3,"王五",20);
		list.add(people1); list.add(people2); list.add(people3);
		
		JRDataSource jrDataSource = new JRBeanCollectionDataSource(list);
		
		File reportFile = new File(getPath(getClass()
				.getResource("jasperPdfTest.class").toString(),"WEB-INF") 
				+ "jasper/" + "demo.jasper");
		
		try {
			request.setCharacterEncoding("utf-8");
	        response.setCharacterEncoding("utf-8");
	        response.setContentType("text/html");
	        response.setContentType("application/pdf");
	        JRAbstractExporter exporter = new JRPdfExporter();
	        JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),
	        		map, jrDataSource);
	        request.getSession().setAttribute(
	                ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
	                jasperPrint);

	        OutputStream out = response.getOutputStream();

	        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
	        exporter.exportReport();
	        out.flush();
			
		} catch (JRException e) {
			e.printStackTrace();
		}
		
	}
	
    public String getPath(String source,String subStr) {
      int start = 0;
      if(source.indexOf("file:") >= 0)
        start = 5;
      return source.substring(start,source.indexOf(subStr));
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
package demo.ireport.vo;

public class People {
	
	private int id;
	private String name;
	private Integer age;
	
	public People(int id, String name, Integer age){
		this.id = id;
		this.name = name;
		this.age = age;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}

}
package demo.ireport.vo;

import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.table.AbstractTableModel;

public class CustomReportData extends AbstractTableModel{

  private String[] columnNames = null;

  private ArrayList data = null;
    

  public CustomReportData(String[] columnNames,ArrayList data)  {
    this.columnNames = columnNames;
    this.data = data;
    
  }

  public int getColumnCount()
  {
    return this.columnNames.length;
  }

  public String getColumnName(int columnIndex)
  {
    return this.columnNames[columnIndex];
  }

  public int getRowCount()
  {
    return this.data.size();
  }

  public Object getValueAt(int rowIndex, int columnIndex)
  {
    HashMap row = (HashMap)this.data.get(rowIndex);
    return row.get(getColumnName(columnIndex));
  }
}

 

四、运行结果

iReport5.5.1+jasperreports5.5.1 制作报表

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/15632.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信