📅  最后修改于: 2020-11-16 07:51:12             🧑  作者: Mango
在上一章中,我们已经了解了如何打印和查看JasperReport生成的文档。在这里,我们将看到如何将这些报告转换或导出为其他格式,例如PDF,HTML和XLS。提供了外观类net.sf.jasperreports.engine.JasperExportManager来实现此功能。导出意味着将JasperPrint对象(.jrprint文件)转换为不同的格式。
以下代码(JasperReportExport.java)演示了JasperReport文档的导出过程。 JasperExportManager提供仅将报告导出为PDF,HTML和XML的方法。要导出为XLS格式,我们使用了类net.sf.jasperreports.engine.export.JRXlsExporter 。这段代码生成以下三个文件-
让我们编写一个报告模板。 JRXML文件的内容(C:\ tools \ jasperreports-5.0.1 \ test \ jasper_report_template.jrxml)如下-
接下来,POJO文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ DataBean.java的内容如下-
package com.tutorialspoint;
public class DataBean {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ DataBeanList.java的内容如下所示-
package com.tutorialspoint;
import java.util.ArrayList;
public class DataBeanList {
public ArrayList getDataBeanList() {
ArrayList dataBeanList = new ArrayList();
dataBeanList.add(produce("Manisha", "India"));
dataBeanList.add(produce("Dennis Ritchie", "USA"));
dataBeanList.add(produce("V.Anand", "India"));
dataBeanList.add(produce("Shrinath", "California"));
return dataBeanList;
}
/**
* This method returns a DataBean object,
* with name and country set in it.
*/
private DataBean produce(String name, String country) {
DataBean dataBean = new DataBean();
dataBean.setName(name);
dataBean.setCountry(country);
return dataBean;
}
}
编写一个主类文件JasperReportFill.java ,该文件从类(DataBeanList)获取Java bean集合并将其传递给JasperReports引擎,以填充报告模板。将其保存到目录C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint 。
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
public class JasperReportFill {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String sourceFileName = "c://tools/jasperreports-5.0.1/"
+ "test/jasper_report_template.jasper";
String printFileName = null;
DataBeanList DataBeanList = new DataBeanList();
ArrayList dataList = DataBeanList.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource =
new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
try {
printFileName = JasperFillManager.fillReportToFile(sourceFileName,
parameters, beanColDataSource);
if (printFileName != null) {
/**
* 1- export to PDF
*/
JasperExportManager.exportReportToPdfFile(printFileName,
"C://sample_report.pdf");
/**
* 2- export to HTML
*/
JasperExportManager.exportReportToHtmlFile(printFileName,
"C://sample_report.html");
/**
* 3- export to Excel sheet
*/
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME,
printFileName);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
"C://sample_report.xls");
exporter.exportReport();
}
} catch (JRException e) {
e.printStackTrace();
}
}
}
在这里,我们包含了将碧玉打印文件导出为pdf,html和xls格式的逻辑。
让我们使用常规的ANT构建过程编译并执行上述文件。 build.xml文件如下所示-
转到命令提示符,然后转到目录build:xml放置在目录C:\ tools \ jasperreports-5.0.1 \ test中。最后,执行命令ant -Dmain-class = com.tutorialspoint.JasperReportFill 。输出如下-
C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=com.tutorialspoint.JasperReportFill
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml
clean-sample:
[delete] Deleting directory C:\tools\jasperreports-5.0.1\test\classes
[delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jasper
[delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrprint
compile:
[mkdir] Created dir: C:\tools\jasperreports-5.0.1\test\classes
[javac] C:\tools\jasperreports-5.0.1\test\baseBuild.xml:28:
warning: 'includeantruntime' was not set, defaulting t
[javac] Compiling 4 source files to C:\tools\jasperreports-5.0.1\test\classes
compilereportdesing:
[jrc] Compiling 1 report design files.
[jrc] log4j:WARN No appenders could be found for logger
(net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
[jrc] log4j:WARN Please initialize the log4j system properly.
[jrc] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[jrc] File : C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.
run:
[echo] Runnin class : com.tutorialspoint.JasperReportFill
[java] log4j:WARN No appenders could be found for logger
(net.sf.jasperreports.extensions.ExtensionsEnvironment).
[java] log4j:WARN Please initialize the log4j system properly.
executereport:
[echo] Im here
BUILD SUCCESSFUL
Total time: 32 seconds
作为上述执行的结果,您将在C:\目录中找到三个文件sample_report.pdf,sample_report.html,sample_report.xls。