简体   繁体   English

如何在 ireport 中创建动态列?

[英]How to create dynamic columns in ireport?

I am having different columns so I want to display dynamic columns in one jasper report.我有不同的列,所以我想在一个 jasper 报告中显示动态列。 But the main thing is columns are varying.但主要的是列是不同的。

Is there any possibilities to write java code in jasper reports?有没有可能在 jasper 报告中编写 java 代码?

Please help me to overcome this problem.请帮助我克服这个问题。

Yes.. you can use Dynamic Jasper for that.是的..你可以使用动态碧玉。 You can add dynamic columns and set all parameters for configuration whatever you want.您可以添加动态列并根据需要设置所有配置参数。 It's easy and simple.这很简单。 Here I am giving some useful class names by which you can achieve the same.在这里,我给出了一些有用的类名,您可以通过它们实现相同的目的。

  • JRDesignBand JR设计乐队
  • JRDesignElementGroup JRDesignElementGroup
  • JRDesignField JRDesignField
  • JRDesignTextField JRDesignTextField
  • JRDesignExpression JRDesignExpression
  • JRDesignSection JR设计部

You don't need to do it in java codes.您不需要在 Java 代码中执行此操作。

You can do it in jrxml file itself.您可以在 jrxml 文件本身中执行此操作。

for example: You need to display 2 column in one jrxml file based on condition.例如:您需要根据条件在一个 jrxml 文件中显示 2 列。

two fields are CASH RECIEPT, CARD RECIEPT.两个字段是现金收据,卡收据。

Put them in almost same position in jrxml file where with site difference to ensure avoid errors (red marked error.)将它们放在 jrxml 文件中几乎相同的位置,其中有站点差异以确保避免错误(红色标记的错误。)

first take CASH RECEIPT先拿现金收据

right click on it and view the properties of it.右键单击它并查看它的属性。

then find the Print When Expression(this is the place where you can apply the condition which should be true to print the column.) and go to it.然后找到 Print When Expression(这是您可以应用应该为真的条件来打印列的地方。)并转到它。

add your expression (condition.) its something like this $F{card}.equals( "0" ) ?添加您的表达式(条件)。它类似于 $F{card}.equals("0") ? true : false this must be changed according to your condition. true : false 这必须根据您的情况进行更改。 HERE WHAT HAPPENS IS WHEN VALUE OF card field is zero in jrxml file it will print the CASH RECEIPT column这里发生的事情是当 jrxml 文件中卡字段的值为零时,它将打印现金收据列

Do the same thing to CARD RECEIPT.对卡收据做同样的事情。

   print when expression  is as follow

                     $F{card}.equals( "1" ) ? true : false

    What will do this is ,CARD RECEIPT will be Printed when card field is hold the value 1.

There are options I used DynamicReports,有一些选项我使用了 DynamicReports,

https://dynamicreports.lbayer.com/examples/examples-overview/ https://dynamicreports.lbayer.com/examples/examples-overview/

You can add columns at runtime.您可以在运行时添加列。

private void printReport() throws DRException {
    AdhocConfiguration configuration = new AdhocConfiguration();
    AdhocReport report = new AdhocReport();

    // column name list
    List<String> columnList = new ArrayList<>();
    columnList.add("Column 1");
    columnList.add("Column 2");
    columnList.add("Column 3");
    columnList.add("Column ...");
    columnList.add("Column n");
    for (String column : columnList) {
        AdhocColumn adhocColumn = new AdhocColumn();
        adhocColumn.setTitle(column);
        adhocColumn.setName(column);
        report.addColumn(adhocColumn);
    }
    configuration.setReport(report);

    String[] myArray = new String[columnList.size()];
    DRDataSource dataSource = new DRDataSource(columnList.toArray(myArray));

    dataSource.add("data1", "data2", "data3", "data ...", "data n");
    dataSource.add("data1", "data2", "data3", "data ...", "data n");

    JasperReportBuilder reportBuilder = AdhocManager.createReport(configuration.getReport());
    reportBuilder.setDataSource(dataSource);
    reportBuilder.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM