/* *该类一定是要public的,不要使用内部类 */ public class Employee { private String name; private Date birthDate =new Date(); private BigDecimal payment = new BigDecimal(23); private BigDecimal bonus =new BigDecimal(24); public Employee(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public BigDecimal getPayment() { return payment; } public void setPayment(BigDecimal payment) { this.payment = payment; } public BigDecimal getBonus() { return bonus; } public void setBonus(BigDecimal bonus) { this.bonus = bonus; } }
public static void main(String[] args) throws ParseException, IOException { InputStream is = new ExportManager().getTemplates("object_collection_template"); List<Employee> employees = generateSampleEmployeeData(); OutputStream os = new FileOutputStream("formulas_output.xlsx"); Context context = new Context(); context.putVar("employees", employees); JxlsHelper.getInstance().processTemplate(is, os, context); } 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")); employees.add(new Employee("Oleg")); return employees; }
导出时候配置的模板如下,具体看自己的项目配置。
新手容易出现的问题: 1.导出路径找不到。自己找原因 2.导出时候只导出了模板,没有导出对应的数据【检查是否设置了area区域】 3.导出时候批注已经没有了,导出数据为空。【检查导出的对象是否是内部类】 4.导出时候excel说是格式不对,不能打开,【检查excel模板是否有多余的字符,不行就重新做一个模板】