有人对dynamicJasper熟悉吗?我正在尝试使用子报表将多个表创建到一个报表中。但是我不知道。我不断出错。我最近遇到的错误之一是
Error: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :
Source text : new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource((java.util.Collection)$P{REPORT_PARAMETERS_MAP}.get( "cs" ) )
注意:我从控制器类中的2个表中获取了我的Collections / data,我用它们来填充表的内容。请帮忙。预先谢谢你。
这是我的报告课:
public class CustomersReportPrintReport {
private final Collection<SalesData> customerlist = new ArrayList<>();
private final Collection<SalesData> loanlist = new ArrayList<>();
private final String fromDate;
private final String toDate;
SalesData salesData = new SalesData();
HashMap params = new HashMap();
JRBeanCollectionDataSource ds1 = new JRBeanCollectionDataSource(customerlist);
JRBeanCollectionDataSource ds2 = new JRBeanCollectionDataSource(loanlist);
public CustomersReportPrintReport(Collection<SalesData> customerList, Collection<SalesData> loanList, LocalDate fromDate, LocalDate toDate) {
//change date format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
String fromDateString = dtf.format(fromDate);
String toDateString = dtf.format(toDate);
//assign variables
this.customerlist.addAll(customerList);
this.loanlist.addAll(loanList);
this.fromDate = fromDateString;
this.toDate = toDateString;
}
public JasperPrint getReport() throws ColumnBuilderException, JRException, ClassNotFoundException, Exception {
Style headerStyle = createHeaderStyle();
Style detailTextStyle = createDetailTextStyle();
Style detailNumberStyle = createDetailNumberStyle();
DynamicReport dynaReport = getReport(headerStyle, detailTextStyle,detailNumberStyle);
//DynamicReport dynaReport2 = createHeaderSubreport(headerStyle, detailTextStyle,detailNumberStyle);
//JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynaReport, new ClassicLayoutManager(), new JRBeanCollectionDataSource(customerlist));
JasperReport jr = DynamicJasperHelper.generateJasperReport(dynaReport, new ClassicLayoutManager(), params, "cs");
JasperPrint jp = JasperFillManager.fillReport(jr, params, ds1);
return jp;
}
private Style createHeaderStyle() {
StyleBuilder sb=new StyleBuilder(true);
sb.setFont(Font.VERDANA_MEDIUM_BOLD);
sb.setBorder(Border.THIN());
sb.setBorderBottom(Border.PEN_2_POINT());
sb.setBorderColor(Color.BLACK);
sb.setBackgroundColor(Color.decode("#007cff"));
sb.setTextColor(Color.WHITE);
sb.setHorizontalAlign(HorizontalAlign.CENTER);
sb.setVerticalAlign(VerticalAlign.MIDDLE);
sb.setTransparency(Transparency.OPAQUE);
return sb.build();
}
private Style createDetailTextStyle(){
StyleBuilder sb=new StyleBuilder(true);
sb.setFont(Font.VERDANA_MEDIUM);
sb.setBorder(Border.DOTTED());
sb.setBorderColor(Color.BLACK);
sb.setTextColor(Color.BLACK);
sb.setHorizontalAlign(HorizontalAlign.LEFT);
sb.setVerticalAlign(VerticalAlign.MIDDLE);
sb.setPaddingLeft(5);
return sb.build();
}
private Style createDetailNumberStyle(){
StyleBuilder sb=new StyleBuilder(true);
sb.setFont(Font.VERDANA_MEDIUM);
sb.setBorder(Border.DOTTED());
sb.setBorderColor(Color.BLACK);
sb.setTextColor(Color.BLACK);
sb.setHorizontalAlign(HorizontalAlign.RIGHT);
sb.setVerticalAlign(VerticalAlign.MIDDLE);
sb.setPattern("#,##0.00");
sb.setPaddingRight(5);
return sb.build();
}
private AbstractColumn createColumn(String property, Class type,
String title, int width, Style headerStyle, Style detailStyle)
throws ColumnBuilderException {
AbstractColumn columnState = ColumnBuilder.getNew()
.setColumnProperty(property, type.getName()).setTitle(
title).setWidth(Integer.valueOf(width))
.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
return columnState;
}
private DynamicReport getReport(Style headerStyle, Style detailTextStyle, Style detailNumStyle) throws ColumnBuilderException, ClassNotFoundException, Exception {
DynamicReportBuilder report=new DynamicReportBuilder();
//make sure column name (1st parameter) is the same with variables from model classes
StyleBuilder titleStyle=new StyleBuilder(true);
titleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
titleStyle.setFont(new Font(20, Font._FONT_GEORGIA, true));
StyleBuilder subTitleStyle=new StyleBuilder(true);
subTitleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
subTitleStyle.setFont(new Font(Font.MEDIUM, Font._FONT_GEORGIA, true));
//set header
report.setTitle("Customer Sales Details Report");
report.setTitleStyle(titleStyle.build());
report.setSubtitle("(" + fromDate + " - " + toDate + ")");
report.setSubtitleStyle(subTitleStyle.build());
report.setUseFullPageWidth(true);
//set page number
report.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT);
//set created on date
//report.addAutoText(AutoText.AUTOTEXT_CREATED_ON, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT,AutoText.PATTERN_DATE_DATE_TIME);
report.addAutoText("Created on " + getDateTime(), AutoText.POSITION_FOOTER, AutoText.ALIGMENT_LEFT, 250);
//page orientation
report.setPageSizeAndOrientation(Page.Page_A4_Landscape());
report.addConcatenatedReport(createSubreport1(headerStyle, detailTextStyle, detailNumStyle), new ClassicLayoutManager(), "cs",
DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,false);
report.addConcatenatedReport(createSubreport2(headerStyle, detailTextStyle, detailNumStyle), "ll",
DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true);
params.put("cs", ds1);
params.put("ll", ds2);
return report.build();
}
private DynamicReport createSubreport1(Style headerStyle, Style detailTextStyle, Style detailNumStyle) throws Exception {
DynamicReportBuilder report=new DynamicReportBuilder();
//make sure column name (1st parameter) is the same with variables from model classes
AbstractColumn columnDate = createColumn("date", String.class,"Date of Orders", 30, headerStyle, detailTextStyle);
AbstractColumn columnInventName = createColumn("inventName", String.class,"Product Ordered", 30, headerStyle, detailTextStyle);
AbstractColumn columnQuantity = createColumn("quantity", Integer.class,"Quantity", 30, headerStyle, detailNumStyle);
AbstractColumn columnAmountPaid = createColumn("amountPaid", Double.class,"Amount Paid", 30, headerStyle, detailNumStyle);
report.addColumn(columnDate)
.addColumn(columnInventName)
.addColumn(columnQuantity)
.addColumn(columnAmountPaid);
report.setUseFullPageWidth(true);
report.setWhenNoDataNoPages();
report.setTitle("Sub Report 1");
return report.build();
}
private JasperReport createSubreport2(Style headerStyle, Style detailTextStyle, Style detailNumStyle) throws Exception {
DynamicReportBuilder report=new DynamicReportBuilder();
DynamicReport dr;
//make sure column name (1st parameter) is the same with variables from model classes
AbstractColumn columnDate = createColumn("date", String.class,"Date", 30, headerStyle, detailTextStyle);
AbstractColumn columnPrice = createColumn("price", Double.class,"Outstanding Balance", 30, headerStyle, detailNumStyle);
report.addColumn(columnDate)
.addColumn(columnPrice);
report.setUseFullPageWidth(true);
report.setWhenNoDataNoPages();
report.setTitle("Sub Report 1");
dr = report.build();
return DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), null);
}
private String getYear() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy");
LocalDateTime now = LocalDateTime.now();
String date = dtf.format(now);
return date;
}
private String getMonth() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM");
LocalDateTime now = LocalDateTime.now();
String date = dtf.format(now);
return date;
}
private String getDateTime() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEEE MMMM dd, yyyy (hh:mm)");
LocalDateTime now = LocalDateTime.now();
String date = dtf.format(now);
return date;
}
}
这是我的控制器类:
private void printAction(ActionEvent event) throws JRException, Exception {
if (fromDate.getValue() != null && toDate.getValue() != null) {
LocalDate fromDt = fromDate.getValue();
LocalDate toDt = toDate.getValue();
CustomersReportPrintReport report = new CustomersReportPrintReport(customerTable.getItems(), loanTable.getItems(), fromDt, toDt);
try {
JasperPrint jp = report.getReport();
JasperViewer jasperViewer = new JasperViewer(jp, false);
jasperViewer.setVisible(true);
} catch (JRException | ColumnBuilderException | ClassNotFoundException ex) {
System.out.println("Error: " + ex);
}
}
}