package com.example.demo.test; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import org.apache.poi.ss.usermodel.Workbook; import org.junit.Test; import java.io.File; import java.io.FileOutputStream; import java.util.*; public class TestExcel { public static void main(String[] args) throws Exception { //模拟从数据库获取需要导出的数据 List<Person> personList = new ArrayList<>(); Person person1 = new Person("路飞","1",new Date()); Person person2 = new Person("娜美","2", new Date()); Person person3 = new Person("索隆","1", new Date()); Person person4 = new Person("小狸猫","1", new Date()); personList.add(person1); personList.add(person2); personList.add(person3); personList.add(person4); //导出操作 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生","学生"), Person .class, personList); FileOutputStream fos = new FileOutputStream("/Users/zhangmuqing/Desktop/test.xlsx"); workbook.write(fos); fos.close(); } @Test public void test1() throws Exception{ List<StudentEntity> studentList = new ArrayList<>(); StudentEntity person1 = new StudentEntity("111","张三0",1,new Date(),new Date()); StudentEntity person2 = new StudentEntity("111","张三2",2,new Date(),new Date()); StudentEntity person3 = new StudentEntity("111","张三1",1,new Date(),new Date()); StudentEntity person4 = new StudentEntity("111","张三3",2,new Date(),new Date()); studentList.add(person1); studentList.add(person2); studentList.add(person3); studentList.add(person4); List<CourseEntity> courselist = new ArrayList<>(); CourseEntity Course1 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList); CourseEntity Course2 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList); CourseEntity Course3 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList); CourseEntity Course4 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList); courselist.add(Course1); courselist.add(Course2); courselist.add(Course3); courselist.add(Course4); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试", "测试"), CourseEntity.class, courselist); FileOutputStream fos = new FileOutputStream("/Users/zhangmuqing/Desktop/test4.xlsx"); workbook.write(fos); fos.close(); } @Test public void test2() throws Exception{ TemplateExportParams params = new TemplateExportParams( "src/main/resources/upload/test模板.xlsx"); Map<String, Object> map = new HashMap<String, Object>(); map.put("date", "2014-12-25"); map.put("money", 2000000.00); map.put("upperMoney", "贰佰万"); map.put("company", "执笔潜行科技有限公司"); map.put("bureau", "财政局"); map.put("person", "JueYue"); map.put("phone", "1879740****"); List<Map<String, String>> listMap = new ArrayList<Map<String, String>>(); for (int i = 0; i < 4; i++) { Map<String, String> lm = new HashMap<String, String>(); lm.put("id", i + 1 + ""); lm.put("zijin", i * 10000 + ""); lm.put("bianma", "A001"); lm.put("mingcheng", "设计"); lm.put("xiangmumingcheng", "EasyPoi " + i + "期"); lm.put("quancheng", "开源项目"); lm.put("sqje", i * 10000 + ""); lm.put("hdje", i * 10000 + ""); listMap.add(lm); } map.put("maplist", listMap); Workbook workbook = ExcelExportUtil.exportExcel(params, map); File savefile = new File("/Users/zhangmuqing/Desktop/"); if (!savefile.exists()) { savefile.mkdirs(); } FileOutputStream fos = new FileOutputStream("/Users/zhangmuqing/Desktop/专项支出用款申请书_map.xlsx"); workbook.write(fos); fos.close(); }
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>3.3.0</version> </dependency>
springboot中以-spring-boot-starter就会自动导入相关的依赖jar包,不用一个一个去导入
}