四、刪除多餘的band,只保留summary,報表的內容全放到這裏
java
五、新建parameter,用來接收數據,必定要記得修改參數類型
六、新建dataset,填好名稱,點擊下一步
工具
七、新建數據源
點擊save,而後點擊下一步
八、點擊下一步
佈局
九、點擊完成,dataset建立完畢
this
十、在java項目中,新建Javabean,用來存數據 spa
public class CategoryBean { private String id; private String series; private String category; private String value; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSeries() { return series; } public void setSeries(String series) { this.series = series; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }十一、將javabean打包成jar包,而且加入到iReport的classpath裏
1三、向報表中添加crosstab組件,選擇剛纔新建的數據源,點擊下一步
1四、設置行數據,選擇series
excel
1五、設置列數據,選擇category
code
1六、設置data,選擇value字段,function選擇nothing
排序
1七、設置佈局,去掉不須要的計算行列total的選項,點擊完成
ip
18,編輯交叉表數據,在添加好的crosstab上點右鍵-Crosstab data
注意選擇合適的數據源類型和表達式
到這裏組件的添加和設置就算完成了,表格的樣式能夠根據須要修改。
特別注意的是要注意勾選Data is pre-sorted,不然數據會從新排序。
1九、點擊編譯生成jasper文件供調用
get
20、新建java類,用來填充數據,調用剛纔生成的jasper文件並導出想要的文件格式,好比pdf,excel等
public class TeamProductivity { public static void main(String[] args) { try { TeamProductivity report=new TeamProductivity(); report.genReport(); } catch (Exception e) { e.printStackTrace(); } } /** * @throws Exception * @Method: genReport * @Author: Liaolz * @Description: 生成報表文件 * * @param * @return void * @throws */ public void genReport() throws Exception { System.out.println("=========開始生成報表========="); long startTime = System.currentTimeMillis(); //準備報表數據 List<CategoryBean> beanDataList = new ArrayList<CategoryBean>(); Map<String, Object> parameters = new HashMap<String, Object>(); setCrosstabData(beanDataList); //主數據源 JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(beanDataList); //交叉表數據源 JRBeanCollectionDataSource dsCrosstab = new JRBeanCollectionDataSource(beanDataList); parameters.put("beanData", dsCrosstab); //導出報表 String jasperFileName="E:\\report\\ireport51\\teamProductivity.jasper"; String exportFileName="e:\\export\\teamProductivity"+startTime; exportReportToExcel(parameters,ds,jasperFileName,exportFileName); System.out.println("=========生成報表完成,所需時間:" + ((System.currentTimeMillis() - startTime)/1000) + "秒========="); } private void setCrosstabData(List<CategoryBean> beanList){ String[] series={"產出箱數","生產小時","成型次數","人頭數","","次品合計","A","B","C","D"}; String[] category={"早班","中班","備註"}; String[][] value={{"730箱","891箱",""}, {"0","0",""}, {"78","78",""}, {"0","0",""}, {"","",""}, {"0","0",""}, {"","","不規則"}, {"","","偏小"}, {"","",""}, {"","",""}}; setBeanList(beanList,series,category,value); } private void setBeanList(List<CategoryBean> beanList,String[] series,String[] category,String[][] value){ int seriesLen=series.length; int categoryLen=category.length; for(int i=0;i<seriesLen;i++){ for(int j=0;j<categoryLen;j++){ CategoryBean dataCrosstab = new CategoryBean(); dataCrosstab.setSeries(series[i]); dataCrosstab.setCategory(category[j]); if(value[i][j]!=null){ dataCrosstab.setValue(value[i][j]); } beanList.add(dataCrosstab); } } } private void exportReportToPDF(Map<String, Object> parameters,JRBeanCollectionDataSource ds,String jasperFileName,String exportFileName)throws Exception{ //生成JasperPrint JasperReport report = (JasperReport)JRLoader.loadObject(new File(jasperFileName)); JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, ds); OutputStream ouputStream = new FileOutputStream(new File(exportFileName+".pdf")); //使用JRPdfExproter導出器導出pdf JRPdfExporter exporter = new JRPdfExporter(); //設置JasperPrintList exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); exporter.exportReport(); ouputStream.close(); } private void exportReportToExcel(Map<String, Object> parameters,JRBeanCollectionDataSource ds,String jasperFileName,String exportFileName)throws Exception{ JasperReport report=(JasperReport)JRLoader.loadObject(new File(jasperFileName)); JasperPrint jasperPrint =JasperFillManager.fillReport(report,parameters, ds); FileOutputStream ouputStream = new FileOutputStream(exportFileName+".xls"); JRAbstractExporter exporter = new JExcelApiExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE); exporter.exportReport(); ouputStream.close(); } }導出的excel截圖
能夠設置多個dataset,生成多個圖表,以下所示