您可从以下链接下载J.J.
rel=“nofollow” http://www.java2s.com/Code/Jar/d/Downloaddynamicreportscore310jar.htm
或 如果你使用ven子,那么你在om子档案中只增加以下依赖物。
<dependency>
<groupId>net.sourceforge.dynamicreports</groupId>
<artifactId>dynamicreports-core</artifactId>
<version>3.1.3</version>
</dependency>
页: 1
<h:commandLink id="summary_jasper" actionListener="#{workReportBean.prepareJasperReport()}">
<p:graphicImage name="/images/jasper.png" title="Jasper"/>
</h:commandLink>
Managed Bean
//Just provide DB_NAME, CREDENTIALS and TABLE_NAME, JASPER report will be created for you
public void prepareJasperReport(){
Map<String, String> columnNameNTypeMap = new HashMap<String, String>();
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/DB_NAME", "USER", "PASSWORD");
ResultSet rsColumns = null;
DatabaseMetaData meta = connection.getMetaData();
rsColumns = meta.getColumns(null, null, "TABLE_NAME", null);
while (rsColumns.next()) {
columnNameNTypeMap.put(rsColumns.getString("COLUMN_NAME"), rsColumns.getString("TYPE_NAME"));
}} catch (SQLException e) {
e.printStackTrace();
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
// a new report
JasperReportBuilder report = DynamicReports.report();
// populating new report with TABLE object
report.setDataSource("select * from TABLE_NAME;", connection);
// creating COLUMNS // add extra datatypes if your table have ex. long, float etc
for (Map.Entry<String, String> entry : columnNameNTypeMap.entrySet()){
if(entry.getValue().equalsIgnoreCase("int")){
report.columns(Columns.column(entry.getKey(), entry.getKey(), DataTypes.integerType()));
}else if(entry.getValue().equalsIgnoreCase("varchar")){
report.columns(Columns.column(entry.getKey(), entry.getKey(), DataTypes.stringType()));
}else if(entry.getValue().equalsIgnoreCase("bit")){
report.columns(Columns.column(entry.getKey(), entry.getKey(), DataTypes.booleanType()));
}else if(entry.getValue().equalsIgnoreCase("datetime") || entry.getValue().equalsIgnoreCase("date")){
report.columns(Columns.column(entry.getKey(), entry.getKey(), DataTypes.dateType()));
}
}
report.title(Components.text("Summary Report").setHeight(40)
.setStyle(DynamicReports.stl.style()
.setBold(true).setFontSize(16).setForegroundColor(Color.BLUE)
.setAlignment(HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE)));
report.setColumnTitleStyle(DynamicReports.stl.style().setBold(true));
report.setColumnStyle(DynamicReports.stl.style().setHorizontalAlignment(HorizontalAlignment.LEFT));
report.setHighlightDetailEvenRows(true);
report.pageFooter(Components.pageXofY());
try {
// show the report
report.show(false);
// export the report to a pdf file
//report.toPdf(new FileOutputStream("d://report.pdf"));
} catch (DRException e) {
e.printStackTrace();
} /*catch (FileNotFoundException e) {
e.printStackTrace();
}*/
}