excle注解JAVA java注解导出excel_dbeaver导出excel文件

作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型企业提供了精准定时调度方案,经受住了生产环境的考验。为使更多童鞋受益,现给出开源框架地址:

https://github.com/sunshinelyz/mykit-delay

PS: 欢迎各位Star源码,也可以pr你牛逼哄哄的代码。      

写在前面

不管是传统软件企业还是互联网企业,不管是管理软件还是面向C端的互联网应用。都不可避免的会涉及到报表操作,而对于报表业务来说,一个很重要的功能就是将数据导出到Excel。如果我们在业务代码中,嵌入很多导出Excel的逻辑,那我们的代码就会变得异常臃肿,不利于维护,而且导出Excel的核心逻辑基本相同。那我们能否将导出Excel的核心逻辑封装成一个工具,当我们需要导出Excel时,只是向工具简单的传入数据呢?于是乎,mykit-excel诞生了!

mykit-excel的github链接地址为:https://github.com/sunshinelyz/mykit-excel 欢迎各位小伙伴Star和Fork源码,也欢迎大家pr你牛逼哄哄的代码,我们一起来养肥它!

框架简述

mykit-excel插件是通用的Excel导入导出框架,旨在提供通用的Excel导入导出功能,支持以注解方式选择JavaBean中的部分字段导出,并提供注解指定Excel列标题和排序功能。

框架结构

  • mykit-excel-annotation: mykit-excel框架的注解模块,提供注解标识类中的哪些字段需要导出到Excel
  • mykit-excel-common: mykit-excel框架的通用工具类,提供通用的工具模板
  • mykit-excel-servlet: mykit-excel框架提供的Web模块,能够支持Web请求导出Excel
  • mykit-excel-springmvc: mykit-excel框架提供的SpringMVC模块,能够支持Web请求导出Excel
  • mykit-excel-test: mykit-excel框架提供的常规测试模块
  • mykit-excel-springboot: mykit-excel框架提供的SpringBoot测试模块

测试用例

(1)测试常规导出Excel工具类的Java类为:io.mykit.excel.springboot.normal.export.TestExportExcelUtils,直接运行该类即可。

(2)测试注解导出Excel工具类的Java类为:io.mykit.excel.springboot.annotation.export.TestAnnotationExportExcelUtils,直接运行该类即可。

(3)测试SpringMVC导出Excel的Java类为io.mykit.excel.springboot.normal.springmvc.NormalExportExcelContorller,运行SpringBoot的启动类io.mykit.excel.springboot.MykitExcelCoreApplication之后,使用resources/html目录下的normalExportExcel.html文件导出Excel即可。如果设置的IP和端口与mykit-excel-springboot模块不同,则修改normalExportExcel.html文件中的IP和端口即可。

(4)测试基于注解导出Java类为io.mykit.excel.springboot.annotation.springmvc.AnnotationExportExcelController,运行SpringBoot的启动类io.mykit.excel.springboot.MykitExcelCoreApplication 之后,使用resources/html目录下的annotationExportExcel.html文件导出Excel即可。如果设置的IP和端口与mykit-excel-springboot模块不同,则修改annotationExportExcel.html文件中的IP和端口即可。

注解说明

如果使用注解方式导出Excel,则需要在JavaBean的属性字段上添加@ExcelColumn注解,此注解中有三个属性,分别如下:

  • isExport:表示是否将当前字段导出到Excel,true:是;false:否
  • title:导出到Excel时的当前列的标题;
  • sort:当前字段导出到Excel的列时,在Excel中的位置,值越小,当前列越靠前。

使用方式

普通方式导出Excel

如果是普通的Java项目,只是将Excel文件导出到本地磁盘,则只需要在项目的pom.xml文件中增加如下配置

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-commonartifactId>
    <version>1.0.0-SNAPSHOTversion>
dependency>

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-commonartifactId>
    <version>1.0.0-SNAPSHOTversion>
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;
    }
}

@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文件即可

public static void main(String[] args) throws Exception{
    ExportExcelUtils utils = new ExportExcelUtils();
    List list = new ArrayList();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", "姓名", "性别" };
    utils.exportExcel("用户导出", columnNames, list, new FileOutputStream("E:/test.xls"), ExportExcelUtils.EXCEL_FILE_2003);
}
public static void main(String[] args) throws Exception{
    ExportExcelUtils utils = new ExportExcelUtils();
    List list = new ArrayList();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", "姓名", "性别" };
    utils.exportExcel("用户导出", columnNames, list, new FileOutputStream("E:/test.xls"), ExportExcelUtils.EXCEL_FILE_2003);
}

导出的文件如下所示



excle注解JAVA java注解导出excel_dbeaver导出excel文件_02

注解方式导出Excel

如果是普通的Java项目,以注解方式将Excel文件导出到本地磁盘,则只需要在项目的pom.xml文件中增加如下配置

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-commonartifactId>
    <version>1.0.0-SNAPSHOTversion>
dependency>

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-commonartifactId>
    <version>1.0.0-SNAPSHOTversion>
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;
    }
}

@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;
    }
}

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

导出的文件如下所示



excle注解JAVA java注解导出excel_excle注解JAVA_03

Web方式导出Excel

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

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-servletartifactId>
    <version>1.0.0-SNAPSHOTversion>
dependency>

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-servletartifactId>
    <version>1.0.0-SNAPSHOTversion>
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;
    }
}

@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 list = new ArrayList();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 util = new ExportExcelWrapper();
    util.exportExcel(fileName, fileName, columnNames, list, response, ExportExcelUtils.EXCEL_FILE_2003);
}
@RequestMapping("/excel")
public void getExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 准备数据
    List list = new ArrayList();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 util = new ExportExcelWrapper();
    util.exportExcel(fileName, fileName, columnNames, list, response, ExportExcelUtils.EXCEL_FILE_2003);
}

导出的文件如下所示



excle注解JAVA java注解导出excel_dbeaver导出excel文件_02

基于注解的Web方式导出Excel

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

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-servletartifactId>
    <version>1.0.0-SNAPSHOTversion>
dependency>

<dependency>
    <groupId>io.mykit.excelgroupId>
    <artifactId>mykit-excel-servletartifactId>
    <version>1.0.0-SNAPSHOTversion>
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;
    }
}

@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;
    }
}

@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 list = new ArrayList();for (int i = 1; i <= 10; i++) {
            list.add(new Student("00" + i, "张三", "001", 100, "篮球"));
        }
        String fileName = "springboot_excel";
        ExportExcelWrapper wrapper = new ExportExcelWrapper();
        wrapper.annotationExportExcel(fileName, fileName, list, Student.class, response, ExportExcelWrapper.EXCEL_FILE_2003);
    }
}
@Controller
@RequestMapping(value = "/annotation/export")
public class AnnotationExportExcelController {

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

导出的文件如下所示



excle注解JAVA java注解导出excel_excle注解JAVA_03

前端测试代码

前端测试代码放在mykit-excel-springboot模块的src/main/resources/html目录下,修改html文件中的连接地址后,将其放在Tomcat或其他Web容器中,进行测试即可。

测试方式

常规测试

直接运行mykit-excel-springboot项目中的io.mykit.excel.springboot.normal.export.TestExportExcelUtils类即可

基于注解的常规测试

直接运行mykit-excel-springboot项目中的io.mykit.excel.springboot.annotation.export.TestAnnotationExportExcelUtils类即可

Web测试

(1)启动mykit-excel-springboot项目,即运行mykit-excel-springboot项目中的io.mykit.excel.springboot.MykitExcelCoreApplication

(2)将mykit-excel-springboot项目的src/main/resources/html下的normalExportExcel.html文件发布到Tomcat等Web容器中访问normalExportExcel.html文件的连接地址, 打开页面点击“Submit”按钮即可。

基于注解的Web测试

(1)启动mykit-excel-springboot项目,即运行mykit-excel-springboot项目中的io.mykit.excel.springboot.MykitExcelCoreApplication类。

(2)将mykit-excel-springboot项目的src/main/resources/html下的annotationExportExcel.html文件发布到Tomcat等Web容器中访问annotationExportExcel.html文件的连接地址, 打开页面点击“Submit”按钮即可。