java|JAVA使用EasyExcel数据库动态导出Excel,超简单!

导入EasyExcel依赖 Gradle

implementation group: 'com.alibaba', name: 'easyexcel', version: '2.2.6'

Maven
com.alibaba easyexcel 2.2.6

demo代码,拿来即用
//保存路径与名字 private final static String fileName = "E:\\" + System.currentTimeMillis() + ".xlsx"; @Test //public static void easyUtil(List heads,String head,List list){public void easyUtil(){ List heads = new ArrayList<>(); //表头信息 heads.add("银行名称"); heads.add("期间新增项目"); heads.add("期间新增担保总额"); heads.add("期间新增解保项目数"); heads.add("期间新增解保金额"); heads.add("在保项目数"); heads.add("在保余额"); heads.add("银行利率"); String head="惠农通数据表"; //主表头 List> list = new ArrayList<>(); Map test = new LinkedHashMap<>(); //手动添加测试数据(可根据需要从数据库查询) test.put("t1", 1); test.put("t2", 2); test.put("t3", 3); test.put("t4", 4); test.put("t5", 5); test.put("t6", 6); test.put("t7", 7); test.put("t8", 8); list.add(test); Map test2 = new LinkedHashMap<>(); test.put("t1", 1); test.put("t2", 2); test.put("t3", 3); test.put("t4", 4); test.put("t5", 5); test.put("t6", 6); test.put("t7", 7); test.put("t8", 8); list.add(test2); List objects = new ArrayList<>(); List> hs = new ArrayList<>(); for (String s : heads) { hs.add(Arrays.asList(head,s)); } //List l = new ArrayList<>(); Collection values; List> list2 = new ArrayList<>(); for (int i = 0; i < list.size(); i++) {values = list.get(i).values(); for (Object value : values) { objects.add(value.toString()); } list2.add(objects); } EasyExcel.write(fileName).head(hs).sheet("模板").doWrite(list2); System.out.println("导出成功"); }
工具类方法如下。拿来即用
//保存路径与名字(也可以放进工具类,通过参数传递) private final static String fileName = "E:\\" + System.currentTimeMillis() + ".xlsx"; /** * 简易版动态导出 * @param heads表头 * @param head主表头 * @param list表数据 */ @Test public static void easyUtil(List heads,String head,List> list){ //public void easyUtil(){List objects = new ArrayList<>(); List> hs = new ArrayList<>(); for (String s : heads) { hs.add(Arrays.asList(head,s)); } //List l = new ArrayList<>(); Collection values; List> list2 = new ArrayList<>(); for (int i = 0; i < list.size(); i++) {values = list.get(i).values(); for (Object value : values) { objects.add(value.toString()); } list2.add(objects); } EasyExcel.write(fileName).head(hs).sheet("模板(sheet名字)").doWrite(list2); System.out.println("导出成功"); }
工具类测试demo
package excel; import com.alibaba.excel.EasyExcel; import java.util.*; /** * @author xu * @Description * @createTime 2021年05月06日 11:45:00 */ public class test {public static void main(String[] args) { List heads = new ArrayList<>(); //表头信息 heads.add("银行名称"); heads.add("期间新增项目"); heads.add("期间新增担保总额"); heads.add("期间新增解保项目数"); heads.add("期间新增解保金额"); heads.add("在保项目数"); heads.add("在保余额"); heads.add("银行利率"); String head="惠农通数据表"; //主表头 List> list = new ArrayList<>(); Map test = new LinkedHashMap<>(); //手动添加测试数据(可根据需要从数据库查询) test.put("t1", 1); test.put("t2", 2); test.put("t3", 3); test.put("t4", 4); test.put("t5", 5); test.put("t6", 6); test.put("t7", 7); test.put("t8", 8); list.add(test); Map test2 = new LinkedHashMap<>(); test.put("t1", 1); test.put("t2", 2); test.put("t3", 3); test.put("t4", 4); test.put("t5", 5); test.put("t6", 6); test.put("t7", 7); test.put("t8", 8); list.add(test2); easyUtil(heads,head,list); }private final static String fileName = "E:\\" + System.currentTimeMillis() + ".xlsx"; public static void easyUtil(List heads,String head,List> list){ //public void easyUtil(){List objects = new ArrayList<>(); List> hs = new ArrayList<>(); for (String s : heads) { hs.add(Arrays.asList(head,s)); } //List l = new ArrayList<>(); Collection values; List> list2 = new ArrayList<>(); for (int i = 0; i < list.size(); i++) {values = list.get(i).values(); for (Object value : values) { objects.add(value.toString()); } list2.add(objects); } EasyExcel.write(fileName).head(hs).sheet("模板(sheet名字)").doWrite(list2); System.out.println("导出成功"); } }
效果如图: java|JAVA使用EasyExcel数据库动态导出Excel,超简单!
文章图片

【java|JAVA使用EasyExcel数据库动态导出Excel,超简单!】如有问题!欢迎大家评论讨论

    推荐阅读