jxls是一个简单的、轻量级的excel导出库,使用特定的标记在excel模板文件中来定义输出格式和布局。java中成熟的excel导出工具备pol、jxl,但他们都是使用java代码的方式来导出excel,编码效率很低且不方便维护。java
另外,jxls2.3的运行效率也至关不错,通过测试,在禁用日志输出的状况下,导出excel单表66535条记录仅仅3000毫秒,与poi几乎没什么大的差距。函数
demo工程源码下载:http://files.cnblogs.com/files/klguang/jxlsdemo.zip工具
excel模板示例:布局
Excel模板标记在jxls中的做用分为三部分:测试
jxls使用 Apache JEXL表达式语言来解析定义在excel模板中的表达式。JEXL与JSTL类似,并对JSTL进行了扩展。eg:ui
${department.chief.age} //属性能够是无限深度编码
${utils:dateFmt(date,"yyyy-MM-dd")} //自定义工具函数lua
XLS Area 是JxlsPlus中的一个重要概念,它表明excel模板中须要被解析的矩形区域,由A1到最后一个单元格表示,有利于加快解析速度。spa
XLS Area 使用excel注释标注表示,它须要被定义在excel 模板的第一个单元格(A1):日志
jx:area(lastCell = "<AREA_LAST_CELL>")
这个标记定义了excel模板须要被解析的矩形区域为:A1到<AREA_LAST_CELL>。
XLS Command 使用excel注释标注表示,命令格式以下:
jx:<command_name>(attr1='val1' attr2='val2' ... attrN='valN' lastCell=<last_cell> areas=["<command_area1>", "<command_area2", ... "<command_areaN>"])
<command_name> 是库自带的命名或是用户自定义并注册到XlsCommentAreaBuilder的命令。
each 命令是最经常使用的XLS命令,形如:
jx:each(items="employees" var="employee" lastCell="D4")
each 能够有以下一些属性:
若是你须要自定jexl来处理数据,你能够从Transformer对象获取JexlEngine引用,并对其配置。
下面的例子实现了将一个自定义jexl函数注册到utils命名空间下:
JxlsHelper jxlsHelper = JxlsHelper.getInstance(); Transformer transformer = jxlsHelper.createTransformer(is, os); JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator(); Map<String, Object> funcs = new HashMap<String, Object>(); funcs.put("utils", new JxlsUtils()); //添加自定义功能 evaluator.getJexlEngine().setFunctions(funcs);
此demo是将简单的员工信息导出excel,demo工程源码下载:http://files.cnblogs.com/files/klguang/jxlsdemo.zip
工程目录:
Employee.java
public class Employee { private String name; private Date birthDate; private BigDecimal payment; private BigDecimal bonus; // getter and setter }
创建excel模板:
工具类JxlsUtils.java
public class JxlsUtils{ private static final String TEMPLATE_PATH="jxls-template"; public static void exportExcel(InputStream is, OutputStream os, Map<String, Object> model) throws IOException{ Context context = new Context(); if (model != null) { for (String key : model.keySet()) { context.putVar(key, model.get(key)); } } JxlsHelper jxlsHelper = JxlsHelper.getInstance(); Transformer transformer = jxlsHelper.createTransformer(is, os); JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator(); Map<String, Object> funcs = new HashMap<String, Object>(); funcs.put("utils", new JxlsUtils()); //添加自定义功能 evaluator.getJexlEngine().setFunctions(funcs); jxlsHelper.processTemplate(context, transformer); } public static void exportExcel(File xls, File out, Map<String, Object> model) throws FileNotFoundException, IOException { exportExcel(new FileInputStream(xls), new FileOutputStream(out), model); } public static void exportExcel(String templateName, OutputStream os, Map<String, Object> model) throws FileNotFoundException, IOException { File template = getTemplate(templateName); if(template!=null){ exportExcel(new FileInputStream(template), os, model); } } //获取jxls模版文件 public static File getTemplate(String name){ String templatePath = JxlsUtils.class.getClassLoader().getResource(TEMPLATE_PATH).getPath(); File template = new File(templatePath, name); if(template.exists()){ return template; } return null; } // 日期格式化 public String dateFmt(Date date, String fmt) { if (date == null) { return ""; } try { SimpleDateFormat dateFmt = new SimpleDateFormat(fmt); return dateFmt.format(date); } catch (Exception e) { e.printStackTrace(); } return ""; } // if判断 public Object ifelse(boolean b, Object o1, Object o2) { return b ? o1 : o2; } }
入口ObjectCollectionDemo.java
public class ObjectCollectionDemo { static Logger logger = LoggerFactory.getLogger(ObjectCollectionDemo.class); public static void main(String[] args) throws ParseException, IOException { logger.info("Running Object Collection demo"); List<Employee> employees = generateSampleEmployeeData(); OutputStream os = new FileOutputStream("target/object_collection_output.xls"); Map<String , Object> model=new HashMap<String , Object>(); model.put("employees", employees); model.put("nowdate", new Date()); JxlsUtils.exportExcel("object_collection_template.xls", os, model); os.close(); } public static List<Employee> generateSampleEmployeeData() throws ParseException { List<Employee> employees = new ArrayList<Employee>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", Locale.US); employees.add( new Employee("Elsa", dateFormat.parse("1970-Jul-10"), 1500, 0.15) ); employees.add( new Employee("Oleg", dateFormat.parse("1973-Apr-30"), 2300, 0.25) ); employees.add( new Employee("Neil", dateFormat.parse("1975-Oct-05"), 2500, 0.00) ); employees.add( new Employee("Maria", dateFormat.parse("1978-Jan-07"), 1700, 0.15) ); employees.add( new Employee("John", dateFormat.parse("1969-May-30"), 2800, 0.20) ); return employees; } }
生成效果: