如何在DynamicReports中获取标题下的列?

时间:2016-07-14 06:19:58

标签: java jasper-reports dynamic-reports

enter image description here

我需要将标题放在标题下方,我之间不需要任何空白。以及如何设置列的宽度?我已经给了setWidth(50),但它没有得到,边框覆盖了页面的整个右侧

这是我的代码:

public class DynamicReport {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/marketing_database","root","root");
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        JasperReportBuilder report = DynamicReports.report();//a new report
        StyleBuilder plainStyle = stl.style().setFontName("FreeUniversal");

        StyleBuilder boldStyle = stl.style(plainStyle).bold().setBorder(stl.pen1Point());
        StyleBuilder col1style = stl.style().setBorder(stl.pen1Point());

        report
          .title(Components.text("Tax Invoice Cum Delivery Challan").setStyle(boldStyle)
          .setHorizontalAlignment(HorizontalAlignment.CENTER))
          .columns(col.column("", "companyName", type.stringType()).setWidth(50).setStyle(col1style))
          .pageFooter(Components.pageXofY())
          .setDataSource("SELECT companyName FROM marketing_database.company_profile", 
                                      connection);
        try {
            report.show();
            report.toPdf(new FileOutputStream("c:/report.pdf"));
        } catch (DRException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      }
}

任何帮助都将受到赞赏

更新:

我得到了如何设置列的宽度。我用了这段代码:

 col.column("", "companyName", type.stringType()).setFixedWidth(20)

1 个答案:

答案 0 :(得分:1)

report



        .columnHeader(//title of the report


              Components.text("Tax Invoice Cum Delivery Challan").setStyle(boldStyle).setFixedHeight(10)
              .setHorizontalAlignment(HorizontalAlignment.CENTER))

             .columns(
                     col.column("", "companyName", type.stringType()).setFixedWidth(200).setStyle(col1style))

              .pageFooter(Components.pageXofY())//show page number on the page footer
              .setDataSource("SELECT companyName FROM marketing_database.company_profile", 
                                      connection);

        try {
                    //show the report
            report.show();

                    //export the report to a pdf file
            report.toPdf(new FileOutputStream("c:/report.pdf"));
        } catch (DRException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      }