软件开发定制公司的报表工具太难用,我三天撸了个Excel工具,运营小姐姐直呼太好用了,现已开源!!(建议收藏)

大家好,我是冰河~~

软件开发定制不管是传统软件企业还软件开发定制是互联网企业,软件开发定制不管是管理软件还是面向C软件开发定制端的互联网应用。软件开发定制都不可避免的会涉及到报表操作,软件开发定制而对于报表业务来说,软件开发定制一个很重要的功能就是软件开发定制将数据导出到Excel。

软件开发定制如果我们在业务代码中,软件开发定制嵌入很多导出Excel的逻辑,软件开发定制那我们的代码就会变得异常臃肿,不利于维护,而且导出Excel的核心逻辑基本相同。那我们能否将导出Excel的核心逻辑封装成一个工具,当我们需要导出Excel时,只是向工具简单的传入数据呢?于是乎,mykit-excel诞生了!

mykit-excel的github链接地址为:

欢迎各位小伙伴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.excel</groupId>    <artifactId>mykit-excel-common</artifactId>    <version>1.0.0-SNAPSHOT</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

创建测试JavaBean

@Datapublic 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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

public static void main(String[] args) throws Exception{    ExportExcelUtils<Student> utils = new ExportExcelUtils<Student>();    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", "姓名", "性别" };    utils.exportExcel("用户导出", columnNames, list, new FileOutputStream("E:/test.xls"), ExportExcelUtils.EXCEL_FILE_2003);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

导出的文件如下所示

注解方式导出Excel

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

<dependency>    <groupId>io.mykit.excel</groupId>    <artifactId>mykit-excel-common</artifactId>    <version>1.0.0-SNAPSHOT</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

创建测试JavaBean

(1) 创建父类JavaBean

@Datapublic 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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(2) 创建子类JavaBean

@Datapublic 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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

接下来,在程序中按照如下方式导出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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

导出的文件如下所示

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>
  • 1
  • 2
  • 3
  • 4
  • 5

创建测试JavaBean

@Datapublic 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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

接下来,在程序中按照如下方式导出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);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

导出的文件如下所示

基于注解的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>
  • 1
  • 2
  • 3
  • 4
  • 5

创建测试JavaBean

(1) 创建父类JavaBean

@Datapublic 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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(2) 创建子类JavaBean

@Datapublic 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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

接下来,在程序中按照如下方式导出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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

导出的文件如下所示

前端测试代码

前端测试代码放在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”按钮即可。

写在最后

如果你想进大厂,想升职加薪,或者对自己现有的工作比较迷茫,都可以私信我交流,希望我的一些经历能够帮助到大家~~

推荐阅读:

  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》
  • 《》

好了,今天就到这儿吧,小伙伴们点赞、收藏、评论,一键三连走起呀,我是冰河,我们下期见~~

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发