我设法通过各种搜索将HtmlComponent
添加到我的项目中。
我的下一个问题是,我不知道如何将其添加到JasperDesign
。
我无法找到任何方法将HtmlComponent
添加到JasperDesign
(甚至不通过群组等)。
我使用Jasper 5.6.1并设法从iReport工具(5.6.0)中获取HtmlComponent jar(5.0.1)。
我的最终目的是能够在报告中包含HTML表格。我知道它不可能以任何方式,但也许,HTML组件将是最有趣的事情。这样,表格的图像就会出现在我的报告中,这没关系。
这是我当前的代码
private JasperDesign getTable(String html, JasperDesign jasperDesign) {
HtmlComponent table = new HtmlComponent();
JRDesignExpression expression = new JRDesignExpression();
expression.setText(html.replace("\n", ""));
table.setHtmlContentExpression(expression);
JRDesignGroup group = new JRDesignGroup();
group.setName(TABLE_GROUP_PREFIX);
JRDesignBand groupHeader = new JRDesignBand();
groupHeader.setHeight(200);
groupHeader.setSplitType(SplitTypeEnum.IMMEDIATE);
groupHeader.addElement(table);
((JRDesignSection) group.getGroupHeaderSection()).addBand(groupHeader);
return jasperDesign;
}
行groupHeader.addElement(table)
失败,因为HtmlComponent不是Element,而是Component。
答案 0 :(得分:1)
要将组件添加到JRDesign
,您需要将其包装在JRDesignComponentElement
JRDesignComponentElement ce = new JRDesignComponentElement(design);
ce.setComponentKey(new ComponentKey("http://jasperreports.sourceforge.net/htmlcomponent", "hc", "html"));
ce.setComponent(table);
//Create the design
JasperDesign design = new JasperDesign();
design.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL); //We will run with no data
design.setName("Html test");
//Add a title band
JRDesignBand title = new JRDesignBand();
title.setHeight(100);
design.setTitle(title);
//Create our component
HtmlComponent hc = new HtmlComponent();
JRDesignExpression expression = new JRDesignExpression();
expression.setText("\"<b>Hello</b> world\"");
hc.setHtmlContentExpression(expression);
//Wrap it in a design componenent
JRDesignComponentElement ce = new JRDesignComponentElement(design);
ce.setComponentKey(new ComponentKey("http://jasperreports.sourceforge.net/htmlcomponent", "hc", "html"));
ce.setComponent(hc);
ce.setHeight(100);
ce.setWidth(100);
title.addElement(ce);
//Compile the design
JasperReport report = JasperCompileManager.compileReport(design);
//Generate the print (passing no datasource, empty)
JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String,Object>());
//Export to pdf
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("pdf/htmlComponent.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
Hello世界的大量代码,在jrxml中执行报告通常更快