1、Jxls不依赖于任何 关于java操作excel的包,亭盍誓叭Jxls定义了一些公共接口分别实现了:Apache POI与Java Excel API想使用Apache POI,需要添加如下依赖:<dependency> <groupId>org.jxls</groupId> <artifactId>jxls-poi</artifactId> <version>1.0.9</version></dependency>想使用Java Excel API,需要添加如下依赖:<dependency> <groupId>org.jxls</groupId> <artifactId>jxls-jexcel</artifactId> <version>1.0.6</version></dependency>
2、创建Excel模板Jxls默认支持Apache JEXL表达式语言,用于在模板中操作Java对象的属性及方法,类似于EL表达式。如图:
3、Jxls利用Excel的批注声明各种命令,上图中:A1单元格的批注:jx:area(lastCell="D4"),定义模板区域是A1:D4A4单元格的批注:jx:each(items="employees" var="employee" lastCell="D4"),定义Each循环命令 ,lastCell="D4 表示Each命令的操作区域是A4:D4
4、导出://加载模板InputStream template诔罨租磊In=this.class.getResourceAsStream("object_collection_template.xls")//创建一个输出流,只要是OutputStream就行,不一定非得用FileOutputStreamOutputStreamos=new FileOutputStream("target/object_collection_output.xls")//设置数据Context context = new Context();List<Employee>employees = .... context.putVar("employees", employees);//导出JxlsHelper.getInstance().processTemplate(templateIn, os, context);代码执行到这里,os 就是你需要的结果,之后是返给前台还是其他处理就随你了