1、创建一个javabean对象
import java.io.Serializable; public class GoodPrint implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String goodName; private String goodNo; private String barcode; private java.math.BigDecimal quantity; private String quantityUnit; public GoodPrint(){ } public GoodPrint(String goodName,String goodNo,String barcode,java.math.BigDecimal quantity,String quantityUnit){ this.goodName=goodName; this.goodNo=goodNo; this.barcode=barcode; this.quantity=quantity; this.quantityUnit=quantityUnit; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } public String getGoodNo() { return goodNo; } public void setGoodNo(String goodNo) { this.goodNo = goodNo; } public String getBarcode() { return barcode; } public void setBarcode(String barcode) { this.barcode = barcode; } public java.math.BigDecimal getQuantity() { return quantity; } public void setQuantity(java.math.BigDecimal quantity) { this.quantity = quantity; } public String getQuantityUnit() { return quantityUnit; } public void setQuantityUnit(String quantityUnit) { this.quantityUnit = quantityUnit; } }
2、工具--》选项--》classpath,导入\WebContent\WEB-INF\classes ,并打钩该选项。
3、创建域字段,如下:
一、用ireport创建数据源,如下:
创建一个工厂类,如下:
public class GoodsFactory { private static GoodPrint[] data={new GoodPrint("GOO1","A","GOO1A",new BigDecimal(10),"M"), new GoodPrint("GOO2","B","GOO2B",new BigDecimal(20),"PCS")}; public static Object[] getBeanArray() { return data; } public static Collection<?> getBeanCollection() { return Arrays.asList(data); } }
导入工厂类
创建Text报表,如下:
二、后台创建数据源如下,如下:
String root_path=request.getRootPath(); String reportFilePath = root_path + "report/Test.jasper"; List goodList=new ArrayList<Goods>(); goodList.add(new GoodPrint("GOO1","A","GOO1A",new BigDecimal(10),"M")); goodList.add(new GoodPrint("GOO2","B","GOO2B",new BigDecimal(20),"PCS")); // JRDataSource dataSource = this.createDataSource(); JRDataSource dataSource = new JRBeanCollectionDataSource(goodList);; // Map<String, Object> parameters = new HashMap<String, Object>(); // List dataList =new ArrayList(); // parameters.put("dataList", dataList); JasperReport report = (JasperReport)JRLoader.loadObject(reportFilePath); JasperPrint jasperPrint = JasperFillManager.fillReport(report, null, dataSource); String repTpl="123"; JasperExportManager.exportReportToPdfFile(jasperPrint,root_path+"report/123.pdf"); // JasperRunManager.runReportToHtmlFile(root_path+"report/123.pdf",null,null); //如果创建报表成功,则转向该报表,其实可以把报表套在框架内,这样实现比较有意义的报表格式。 response.sendRedirect("report2.html"); // JasperFillManager.fillReport(report, parameters, dataSource); // OutputStream ouputStream = response.getOutputStream(); // response.setContentType("application/pdf"); // response.setCharacterEncoding("UTF-8"); // String repName=URLEncoder.encode("物料报表", "UTF-8"); // response.setHeader("Content-Disposition", "attachment; filename=\""+repName+"\".pdf"); // // // 使用JRPdfExproter导出器导出pdf // JRPdfExporter exporter = new JRPdfExporter(); // exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); // exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); // exporter.exportReport(); // ouputStream.close();
预览结果,如下: