jasperireport 入门级 Demo之导出Excel

1.下载:iReport-4.0.2-windows-installer.exe

2.需要jar包:   点此下载

3.用iReport软件画报表,如图:

 

4.将生成的jasper文件拷贝至项目中:

编写如下程序读取并导出excel,

package action;

import jasperireport.service.Beans;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

public class Report extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("fid", "编号");  
		map.put("fname", "姓名");
		
		JRBeanCollectionDataSource ds=new JRBeanCollectionDataSource(this.myList());
		List<JasperPrint> JasperPrintlist=new ArrayList<JasperPrint>();
		try {
			JasperPrint js=JasperFillManager.fillReport(request.getSession().getServletContext().getRealPath("jasper")+File.separator +"iDemo.jasper", map, ds);
			JasperPrintlist.add(js);
		} catch (JRException e) {
			e.printStackTrace();
		}
		  ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		  JRXlsExporter exporter = new JRXlsExporter();
		  exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,JasperPrintlist);
		  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
		  exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
		  exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
		  exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
		  try {
			exporter.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}
		byte[] bytes = oStream.toByteArray();
		
		response.setHeader("Content-disposition","attachment; filename= aa.xls");    
		response.setContentType("application/vnd.ms-excel");
		response.setContentLength(bytes.length);
		ServletOutputStream os = response.getOutputStream();
		os.write(bytes, 0, bytes.length);
		os.flush();
		os.close();
	}
	
	
	//要导出的集合[初始化测试数据]
	public List<Beans> myList(){
		List<Beans> list = new ArrayList<Beans>();	
		Beans bean = new Beans();
		bean.setFid("1");
		bean.setFname("黄");
		
		Beans bean2 = new Beans();
		bean2.setFid("2");
		bean2.setFname("禹");
		
		Beans bean3 = new Beans();
		bean3.setFid("3");
		bean3.setFname("舜");
		list.add(bean);
		list.add(bean2);
		list.add(bean3);
		return list;
	}
	
}

5.完成,此demo可供没接触过该技术的同学参考。至于扩展自己发挥。

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值