4. 设置classpath路径,我这里自己新建了一个class文件夹,用于存放class文件
说明:iReport可以基于jdbc数据源和javaBean两种方式建立模版,这里介绍javaBean方式,所以需要javaBean的.class文件
5. 点击所示图标,配置field
输入完整类名,添加字段
6.自定义模版,配置简单的模版还是比较简单的,我这里直接打开了我自己测试用的模版,点击所示图标 编译之,可生成 .jasper 文件,这个文件就是我们真正需要用的模版(会生成在当前文件夹)
以上介绍了iReport简单使用,以后可能会添加一些细节在上面,今天先粗略的到这里,接下来说 JasperReport 结合 模版 生成excle报表;
直接上代码吧
1. maven依赖
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>net.sf.jasperreportsgroupId>
<artifactId>jasperreportsartifactId>
<version>6.3.0version>
dependency>
<dependency>
<groupId>org.codehaus.groovygroupId>
<artifactId>groovy-allartifactId>
<version>2.4.5version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>3.12version>
dependency>
dependencies>
2.javaBean
package com.mxh1995.pro.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String clazz;
private Integer age
public Student(){}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
...省略setter/getter方法
}
3.JasperReport
package com.mxh1995.pro.jsperTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.time.DateFormatUtils;
import com.mxh1995.pro.test.Student;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
public class JsperTest{
public static void main(String[] args) {
try {
new JsperTest().executeExport();
} catch (Exception e) {
e.printStackTrace();
}
}
public void executeExport() throws Exception {
//常见的应用是从数据库中读取数据,然后封装成Collection,这里模拟从数据库中读取数据
List result = new ArrayList();
Student s1 = new Student(1, "name1", 12);
Student s2 = new Student(2, "name2", 22);
Student s3 = new Student(3, "name3", 32);
Student s4 = new Student(4, "name4", 42);
result.add(s4);
result.add(s3);
result.add(s2);
result.add(s1);
File reportFile = new File("F:\\report1.jasper");
if (!reportFile.exists()){
throw new Exception("传入的模板文件不存在!");
}
InputStream is = new FileInputStream(reportFile);
Map parameters = new HashMap();// 空的参数
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(result);//数据源
JasperPrint jasperPrint = JasperFillManager.fillReport(is, parameters, ds);//填充
//导出文件存放路径
String dateTime = DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
File destFile = new File("student" + dateTime + ".xls");
//设置输入输出流--执行导出
JRXlsExporter exporter = new JRXlsExporter(); // Excel
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(false);
configuration.setDetectCellType(true);// 检查单元格格式
exporter.setConfiguration(configuration);
exporter.exportReport();
}
}
如有错误,欢迎指正!