赞
踩
不管是传统软件企业还是互联网企业,不管是管理软件还是面向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列标题和排序功能。
(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注解,此注解中有三个属性,分别如下:
如果是普通的Java项目,只是将Excel文件导出到本地磁盘,则只需要在项目的pom.xml文件中增加如下配置
<dependency>
<groupId>io.mykit.excel</groupId>
<artifactId>mykit-excel-common</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文件即可
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);
}
导出的文件如下所示
如果是普通的Java项目,以注解方式将Excel文件导出到本地磁盘,则只需要在项目的pom.xml文件中增加如下配置
<dependency>
<groupId>io.mykit.excel</groupId>
<artifactId>mykit-excel-common</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文件即可
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);
}
}
导出的文件如下所示
如果是基于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);
}
导出的文件如下所示
如果是基于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); } }
导出的文件如下所示
前端测试代码放在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
类即可
(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”按钮即可。
(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”按钮即可。
好了,今天就到这儿吧,我是冰河,我们下期见!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。