请先阅读 “Java+POI+模板”一:打造复杂Excel 报表
1. 首先创建ExcelUtil类,该类将要实现将一组对象转换为Excel表格,并且可以从Excel表格中读取到一组List对象中。现在我们先创建类并实现单例。
public class ExcelUtil { private static ExcelUtil eu = new ExcelUtil(); private ExcelUtil(){} public static ExcelUtil getInstance() { return eu; } }
2. 创建一个Annotation类,用来在对象的get方法上加入的annotation,通过该annotation说明某个属性所对应的标题。
@Retention(RetentionPolicy.RUNTIME) public @interface ExcelResources { /** * 属性的标题名称 * @return */ String title(); /** * 在excel的顺序 * @return */ int order() default 9999; }
3. 创建ExcelHeader类,用来存储Excel标题的对象,通过该对象可以获取标题和方法的对应关系
public class ExcelHeader implements Comparable<ExcelHeader>{ /** * excel的标题名称 */ private String title; /** * 每一个标题的顺序 */ private int order; /** * 说对应方法名称 */ private String methodName; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getOrder() { return order; } public void setOrder(int order) { this.order = order; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public int compareTo(ExcelHeader o) { return order>o.order?1:(order<o.order?-1:0); } public ExcelHeader(String title, int order, String methodName) { super(); this.title = title; this.order = order; this.methodName = methodName; } @Override public String toString() { return "ExcelHeader [title=" + title + ", order=" + order + ", methodName=" + methodName + "]"; } }
4. 创建Model类,并在属性的get方法上面加上我们的Annotation
public class User { private int id; private String username; private String nickname; private int age; @ExcelResources(title="用户标识",order=1) public int getId() { return id; } public void setId(int id) { this.id = id; } @ExcelResources(title="用户名",order=2) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @ExcelResources(title="用户昵称",order=3) public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @ExcelResources(title="用户年龄",order=4) public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(int id, String username, String nickname, int age) { super(); this.id = id; this.username = username; this.nickname = nickname; this.age = age; } public User() { super(); } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", nickname=" + nickname + ", age=" + age + "]"; } }
5. 在ExcelUtil中实现exportObj2ExcelByTemplate方法,用于处理将对象转换为Excel。
public void exportObj2ExcelByTemplate(Map<String,String> datas,String template,String outPath, List objs, Class clz, boolean isClasspath){ ExcelTemplate et=ExcelTemplate.getInstance(); if(isClasspath){ et.readTemplateByClasspath(template); }else{ et.readTemplateByPath(template); } List<ExcelHeader> headers=getHeaderList(clz); Collections.sort(headers); //输出标题 et.createNewRow(); for (ExcelHeader eh : headers) { et.createCell(eh.getTitle()); } //输出值 try { for(Object obj:objs){ et.createNewRow(); for(ExcelHeader eh:headers){ String mn=eh.getMethodName(); Method m=clz.getDeclaredMethod(mn); Object rel=m.invoke(obj); et.createCell(rel.toString()); } } } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } et.writeToFile(outPath); }
这里输出值的过程也可以通过导入BeanUtils来处理
//输出值 try { for(Object obj:objs){ et.createNewRow(); for(ExcelHeader eh:headers){ // String mn=eh.getMethodName(); // Method m=clz.getDeclaredMethod(mn); // Object rel=m.invoke(obj); // et.createCell(rel.toString()); et.createCell(BeanUtils.getProperty(obj,getMethodName(eh) )); } } }
exportObj2ExcelByTemplate方法方法会调用getHeaderList方法,来获取Annotation的title以及order
private List<ExcelHeader> getHeaderList(Class clz){ List<ExcelHeader> headers=new ArrayList<ExcelHeader>(); Method[] ms = clz.getDeclaredMethods(); for (Method m : ms) { String mn =m.getName(); if(mn.startsWith("get")&& m.isAnnotationPresent(ExcelResource.class)){ ExcelResource er=m.getAnnotation(ExcelResource.class); headers.add(new ExcelHeader(er.title(), er.order(), mn)); } } return headers; }
6. 实现此功能
@Test public void testObj2Xls() { List<User> users = new ArrayList<User>(); users.add(new User(1,"aaa","水水水",11)); users.add(new User(2,"sdf","水水水",11)); users.add(new User(3,"sdfde","水水水",11)); users.add(new User(4,"aaa","水水水",11)); users.add(new User(54,"aaa","水水水",11)); users.add(new User(16,"aaa","水水水",11)); ExcelUtil.getInstance().exportObj2ExcelByTemplate(null,"/excel/user.xls","d:/test/poi/tus.xls", users, User.class, true); }
原先的模板如下图:
插入完成后的模板如下图:
相关推荐
NULL 博文链接:https://weigang-gao.iteye.com/blog/2103017
springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式...
java+poi+excel读取写入等操作所有jar(最新支持excel2013)Java实用源码整理learns
java基于poi使用excel模板导出
poi基于模板导出excel,poi基于模板导出excelpoi,基于模板导出excel
Freemarker+poi 模板生成word+导入图片 直接导入eclipse即可,希望对大家有所帮助
java+poi+excel读取写入等操作所有jar(最新支持excel2013)+poi-bin-3.10-beta2-20130904源代码学习
SpringBoot +Mybatis +POI导入、导出Excel文件 简单清晰几个类,处理SpringBoot 导入导出excel doc目录下 是mysql数据库脚本 和 导入的模板
springboot + poi导出指定格式Excel模板。 springboot+poi导出指定格式Excel模板,完整项目,导出即用。 poi导出Excel
java基于poi通过excel模板导出excel文档,整理了一下,收个一分,以示安慰吧
poi 基于excel模板导出 功能强大 代码清晰
poi 操作excel模板,读取数据后,讲数据填充到新生成的文件中且提供下载
POI使用excel模板循环输出行到文件并导出的一个小例子
java+poi+excel读取写入等操作所有jar(最新支持excel2013)+poi-bin-3.10-beta2-20130904javalearnsa资料分享
Java POI下载Excel模板 Excel带有下拉框的模板,并有详细注释,生成exlcel的下拉框,必填项标红,数字栏位防止科学计数法,并设置Excel标题样式
主要是springboot+myBatis+poi+mysql的使用,从数据库查询到结果集导出excel到本地,从本地中的excel文件导入到数据库中。
springboot+mybatis+poi 实现excel导入导出操作
Java POI根据模板生成Excel文件并写入磁盘,资源文件仅为实现的简单测试Demo,并没有进行代码优化,可以直接导入运行,资源文件仅供参考。
通用的java+poi读取excel数据 源码,包含有poi.jar包。如需读取excel2003以上版本,只需修改HSSF为XSSF。
java poi 根据excel模板生成excel文件,利用poijar包根据已有excel模板,生成基于模板的新文件,jar包也在压缩包内,导入即可使用。