`
Luob.
  • 浏览: 1588815 次
  • 来自: 上海
社区版块
存档分类
最新评论

DynamicReports 简单使用

阅读更多
效果图


public void export(@Valid PaymentChartQuery dto, HttpServletRequest request, HttpServletResponse response){
        List<ColumnBuilder> colms=new ArrayList<>();

        colms.add(col.reportRowNumberColumn("序号").setHorizontalTextAlignment(HorizontalTextAlignment.CENTER));
        colms.add(col.column("销售单号","fsSellNo",type.stringType()));
        colms.add(col.column("日期","fsSellDate",type.stringType()));
        colms.add(col.column("结账时间","fsCheckEndTime",type.stringType()));
        colms.add(col.column("付款金额","fdReceMoney",type.bigDecimalType()));
        colms.add(col.column("付款方式","fsPaymentName",type.stringType()));
        colms.add(col.column("班别","fsShiftName",type.stringType()));
        colms.add(col.column("账单号","fsCheckBillNo",type.stringType()));
        colms.add(col.column("收银员","fsCashier",type.stringType()));
        colms.add(col.column("备注","fsNote",type.stringType()));

        String filename= "付款明细表";
        try {
            filename = new String(filename.getBytes(), "iso8859-1");//解决中文 文件名问题
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+filename+".xls");
            //outstream
            OutputStream outputStream = response.getOutputStream();

            JasperXlsExporterBuilder xlsExporter = export.xlsExporter(outputStream).addSheetName("付款明细表")
                    .setDetectCellType(true).setIgnorePageMargins(true).setWhitePageBackground(false)
                    .setRemoveEmptySpaceBetweenColumns(true);

            dto.setPrint(true);
            Map<String,Object> map=paymentChartService.queryPaymentChart(dto);
            List<PaymentChartDTO> list=(List)map.get(ResultCode.DATA);
            List<PaymentChartTotalDTO> total= (List)map.get(ResultCode.TOTAL);
            SubreportBuilder subreport1 = cmp.subreport(createTotal(total)).setFixedWidth(700);

            JasperReportBuilder report=report();
            report.setTemplate(Templates.reportTemplate)
                    .ignorePageWidth()
                    .title(cmp.horizontalList(subreport1),cmp.verticalGap(20))
                    .columns(colms.toArray(new ColumnBuilder[colms.size()]))
                    .setHighlightDetailEvenRows(true)           //偶数行高亮显示
                    .highlightDetailEvenRows()
                    .setDataSource(list)
                    .toXls(xlsExporter);

            outputStream.flush();
            outputStream.close();
        }catch (IOException e) {
            e.printStackTrace();
        }catch (DRException e) {
            e.printStackTrace();
        }
    }

    /**
     * 合计
     * @param list
     * @return
     */
    private JasperReportBuilder createTotal(List<PaymentChartTotalDTO> list){
        JasperReportBuilder report = report();
        PaymentChartTotalDTO total= list.remove(0); //总计:

        List<ColumnBuilder> colms=new ArrayList<>();
        TextColumnBuilder<String> fsPaymentName_col=col.column("付款方式","fsPaymentName",type.stringType()).setWidth(100);
        TextColumnBuilder<BigDecimal> fdReceMoney_col=col.column("金额","fdReceMoney",type.bigDecimalType()).setWidth(100);

        colms.add(col.reportRowNumberColumn("序号").setHorizontalTextAlignment(HorizontalTextAlignment.CENTER));
        colms.add(fsPaymentName_col);
        colms.add(fdReceMoney_col);
        colms.add(col.percentageColumn("比例 %", fdReceMoney_col));

        //饼图
        PieChartBuilder pie_payment = cht.pieChart()
                .setTitle("payment chart")
                .setTitleFont(stl.fontArialBold())
                .setKey(fsPaymentName_col).setShowLegend(false)
                .series(cht.serie(fdReceMoney_col));

        //列表
        report.setTemplate(Templates.reportTemplate_2)
                .columns(colms.toArray(new ColumnBuilder[colms.size()]))
                .subtotalsAtSummary(sbt.text("合计:",fsPaymentName_col),sbt.sum(fdReceMoney_col))
                .setDataSource(list);

        SubreportBuilder subreport1 = cmp.subreport(report);

        JasperReportBuilder report1=report();
        report1.setTemplate(Templates.reportTemplate_2)
                .summary(cmp.horizontalList(pie_payment,subreport1))
                .setDataSource(list);
        return report1;
    }
  • 大小: 106.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics