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可供没接触过该技术的同学参考。至于扩展自己发挥。