无法从DynamicReports生成报告

时间:2015-01-28 10:47:19

标签: java dynamic-reports

我提到了this链接,通过servlet

在Web浏览器中生成报告

但我的代码无效

这是我的代码

           response.setContentType("application/pdf");
           OutputStream outputstream = response.getOutputStream();
        try {
            StyleBuilder boldStyle         = stl.style().bold(); 

            StyleBuilder boldCenteredStyle = stl.style(boldStyle)

                                                .setHorizontalAlignment(HorizontalAlignment.CENTER);

            StyleBuilder columnTitleStyle  = stl.style(boldCenteredStyle)

                                                .setBorder(stl.pen1Point())

                                                .setBackgroundColor(Color.LIGHT_GRAY);

            StyleBuilder CenteredStyle = stl.style().setHorizontalAlignment(HorizontalAlignment.CENTER);
            StyleBuilder titleStyle         = stl.style(boldStyle).setFontSize(15);
            JasperReportBuilder report = DynamicReports.report();//a new report
            TextColumnBuilder<Integer> rowNumberColumn = col.reportRowNumberColumn("No.").setStyle(CenteredStyle);
            TextColumnBuilder<java.util.Date> columndate = col.column("Date", "Date_Of_Sale", type.dateType()).setStyle(CenteredStyle);
            TextColumnBuilder<String>    columnItem = col.column("Item Name", "Item_Name", type.stringType()).setStyle(boldStyle);
            TextColumnBuilder<String>    columnCustomer = col.column("Customer Name", "Customer_name", type.stringType()).setStyle(CenteredStyle);
            TextColumnBuilder<Double>    columnunit = col.column("Unit Price", "Item_Price", type.doubleType()).setStyle(CenteredStyle);
            TextColumnBuilder<Integer>    columnqty = col.column("Qty", "Item_qty", type.integerType()).setStyle(CenteredStyle);
            TextColumnBuilder<BigDecimal>    columnsub = columnqty.multiply(columnunit).setTitle("Subtotal").setStyle(CenteredStyle);
            ColumnGroupBuilder itemGroup = grp.group(columnItem);

            report
              .columns(
                      rowNumberColumn,columnCustomer,columndate,columnunit,columnqty,columnsub
                      )
              .title(//title of the report
                  Components.text("Sale Report").setStyle(titleStyle))
                  .setColumnTitleStyle(columnTitleStyle)
                  .highlightDetailEvenRows()

                  .pageFooter(Components.pageXofY().setStyle(boldCenteredStyle))//show page number on the page footer
                  .setDataSource(createDataSource(from,to));      

            report.groupBy(itemGroup).subtotalsAtFirstGroupFooter(sbt.sum(columnunit).setStyle(boldCenteredStyle),sbt.sum(columnsub).setStyle(boldCenteredStyle))
            .subtotalsAtSummary(sbt.sum(columnunit).setStyle(boldCenteredStyle),sbt.sum(columnsub).setStyle(boldCenteredStyle));


            report.toPdf(outputstream);
        }
catch(DRException e)
        {
            e.printStackTrace();
        }
        outputstream.flush();
        outputstream.close();

我正在填充数据库中的报告,该报告工作正常,但报告未显示在Web浏览器

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,以下代码为我工作。

OutputStream outputStream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment;filename="+filename);
report.toPdf(OutputStream);
OutputStream.flush();
OutputStream.close();