大家好,我是冰河~~
不管是傳統軟體企業還是互聯網企業,不管是管理軟體還是面向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.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);
}
匯出的檔案如下所示

注解方式匯出Excel
如果是普通的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);
}
}
匯出的檔案如下所示

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);
}
匯出的檔案如下所示

基于注解的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);
}
}
匯出的檔案如下所示

前端測驗代碼
前端測驗代碼放在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”按鈕即可,
寫在最后
如果你想進大廠,想升職加薪,或者對自己現有的作業比較迷茫,都可以私信我交流,希望我的一些經歷能夠幫助到大家~~
推薦閱讀:
- 《實踐出真知:全網最強秒殺系統架構解密,不是所有的秒殺都是秒殺!!》
- 《從零到上億用戶,我是如何一步步優化MySQL資料庫的?(建議收藏)》
- 《我用多執行緒進一步優化了億級流量電商業務下的海量資料校對系統,性能再次提升了200%!!(全程干貨,建議收藏)》
- 《我用多執行緒優化了億級流量電商業務下的海量資料校對系統,性能直接提升了200%!!(全程干貨,建議收藏)》
- 《我用10張圖總結出了這份并發編程最佳學習路線!!(建議收藏)》
- 《高并發場景下一種比讀寫鎖更快的鎖,看完我徹底折服了!!(建議收藏)》
- 《全網最全性能優化總結!!(冰河吐血整理,建議收藏)》
- 《三天擼完了MyBatis,各位隨便問!!(冰河吐血整理,建議收藏)》
- 《奉勸那些剛參加作業的學弟學妹們:要想進大廠,這些并發編程知識是你必須要掌握的!完整學習路線!!(建議收藏)》
- 《奉勸那些剛參加作業的學弟學妹們:要想進大廠,這些核心技能是你必須要掌握的!完整學習路線!!(建議收藏)》
- 《奉勸那些剛參加作業的學弟學妹們:這些計算機與作業系統基礎知識越早知道越好!萬字長文太頂了!!(建議收藏)》
- 《我用三天時間開發了一款老少皆宜的國民級游戲,支持播放音樂,現開放完整源代碼和注釋(建議收藏)!!》
- 《我是全網最硬核的高并發編程作者,CSDN最值得關注的博主,大家同意嗎?(建議收藏)》
- 《畢業五年,從月薪3000到年薪百萬,我掌握了哪些核心技能?(建議收藏)》
- 《我入侵了隔壁妹子的Wifi,發現,,,(全程實戰干貨,建議收藏)》
- 《千萬不要輕易嘗試“熊貓燒香”,這不,我后悔了!》
- 《清明節偷偷訓練“熊貓燒香”,結果我的電腦為熊貓“獻身了”!》
- 《7.3萬字肝爆Java8新特性,我不信你能看完!(建議收藏)》
- 《在業務高峰期拔掉服務器電源是一種怎樣的體驗?》
- 《全網最全Linux命令總結!!(史上最全,建議收藏)》
- 《用Python寫了個工具,完美破解了MySQL!!(建議收藏)》
- 《SimpleDateFormat類到底為啥不是執行緒安全的?(附六種解決方案,建議收藏)》
- 《MySQL 8中新增的這三大索引,直接讓MySQL起飛了,你竟然還不知道!!(建議收藏)》
- 《擼完Spring原始碼,我開源了這個分布式快取框架!!(建議收藏)》
- 《億級流量高并發秒殺系統商品“超賣”了,只因使用的JDK同步容器中存在這兩個巨大的坑!!(踩坑實錄,建議收藏)》
- 《奉勸那些剛參加作業的學弟學妹們:要想學好并發編程,這些并發容器的坑是你必須要注意的!!(建議收藏)》
好了,今天就到這兒吧,小伙伴們點贊、收藏、評論,一鍵三連走起呀,我是冰河,我們下期見~~

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299143.html
標籤:其他
