我把这个贼好用的Excel导出工具开源了!! (2)

(2) 创建子类JavaBean

@Data public class Student extends Person{ private static final long serialVersionUID = -6180523202831503132L; @ExcelColumn(isExport = false, title = "班级编号", sort = 1) private String classNo; private Integer score; @ExcelColumn(isExport = true, title = "爱好", sort = 5) private String hobby; public Student(){ } public Student(String id, String name, String classNo, Integer score, String hobby){ super(id, name); this.classNo = classNo; this.score = score; this.hobby = hobby; } }

接下来,在程序中按照如下方式导出Excel文件即可

public class TestAnnotationExportExcelUtils { public static void main(String[] args) throws FileNotFoundException { // 准备数据 List<Student> list = new ArrayList<Student>(); for (int i = 1; i <= 10; i++) { list.add(new Student("00" + i, "张三", "001", 100, "篮球")); } AnnotationExcelExportUtils<Student> utils = new AnnotationExcelExportUtils<Student>(); utils.exportExcel("用户导出", list, new FileOutputStream("e:/E:/test.xls"), Student.class, AnnotationExcelExportUtils.EXCEL_FILE_2003); } }

导出的文件如下所示

我把这个贼好用的Excel导出工具开源了!!

Web方式导出Excel

如果是基于Java Web或Spring MVC项目,需要导出Excel,则需要在项目的pom.xml文件中,加入如下配置

<dependency> <groupId>io.mykit.excel</groupId> <artifactId>mykit-excel-servlet</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>

创建测试JavaBean

@Data public class Student implements Serializable { private static final long serialVersionUID = -2987207599880734028L; private int id; private String name; private String sex; public Student(){ } public Student(int id, String name, String sex){ this.id = id; this.name = name; this.sex = sex; } }

接下来,在程序中按照如下方式导出Excel文件即可

@RequestMapping("/excel") public void getExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { // 准备数据 List<Student> list = new ArrayList<Student>(); for (int i = 0; i < 10; i++) { list.add(new Student(111,"张三","男")); list.add(new Student(111,"李四","男")); list.add(new Student(111,"王五","女")); } String[] columnNames = { "ID", "姓名", " 性别"}; String fileName = "springboot_excel"; ExportExcelWrapper<Student> util = new ExportExcelWrapper<Student>(); util.exportExcel(fileName, fileName, columnNames, list, response, ExportExcelUtils.EXCEL_FILE_2003); }

导出的文件如下所示

我把这个贼好用的Excel导出工具开源了!!

基于注解的Web方式导出Excel

如果是基于Java Web或Spring MVC项目,需要基于注解导出Excel,则需要在项目的pom.xml文件中,加入如下配置

<dependency> <groupId>io.mykit.excel</groupId> <artifactId>mykit-excel-servlet</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>

创建测试JavaBean

(1) 创建父类JavaBean

@Data public class Person implements Serializable { private static final long serialVersionUID = 3251965335162340137L; @ExcelColumn(isExport = true, title = "编号", sort = 2) private String id ; @ExcelColumn(isExport = true, title = "姓名", sort = 3) private String name; public Person(){ } public Person(String id, String name){ this.id = id; this.name = name; } }

(2) 创建子类JavaBean

@Data public class Student extends Person{ private static final long serialVersionUID = -6180523202831503132L; @ExcelColumn(isExport = false, title = "班级编号", sort = 1) private String classNo; private Integer score; @ExcelColumn(isExport = true, title = "爱好", sort = 5) private String hobby; public Student(){ } public Student(String id, String name, String classNo, Integer score, String hobby){ super(id, name); this.classNo = classNo; this.score = score; this.hobby = hobby; } }

接下来,在程序中按照如下方式导出Excel文件即可

@Controller @RequestMapping(value = "/annotation/export") public class AnnotationExportExcelController { @RequestMapping("/excel") public void getExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { // 准备数据 List<Student> list = new ArrayList<Student>(); for (int i = 1; i <= 10; i++) { list.add(new Student("00" + i, "张三", "001", 100, "篮球")); } String fileName = "springboot_excel"; ExportExcelWrapper<Student> wrapper = new ExportExcelWrapper<Student>(); wrapper.annotationExportExcel(fileName, fileName, list, Student.class, response, ExportExcelWrapper.EXCEL_FILE_2003); } }

导出的文件如下所示

我把这个贼好用的Excel导出工具开源了!!

前端测试代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsfpwy.html