【springboot+easypoi】一行代码搞定excel导入导出
生活随笔
收集整理的這篇文章主要介紹了
【springboot+easypoi】一行代码搞定excel导入导出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:https://www.jianshu.com/p/5d67fb720ece
?
?
- 開發中經常會遇到excel的處理,導入導出解析等等,java中比較流行的用poi,但是每次都要寫大段工具類來搞定這事兒,此處推薦一個別人造好的輪子【easypoi】,下面介紹下“輪子”的使用。
pom引入
- 不再需要其他jar
編寫實體類
- 此處注意必須要有空構造函數,否則會報錯“對象創建錯誤”
- 關于注解@Excel,其他還有@ExcelCollection,@ExcelEntity ,@ExcelIgnore,@ExcelTarget等,此處我們用不到,可以去官方查看更多
| name | String | null | 列名 |
| needMerge | boolean | fasle | 縱向合并單元格 |
| orderNum | String | "0" | 列的排序,支持name_id |
| replace | String[] | {} | 值得替換 導出是{a_id,b_id} 導入反過來 |
| savePath | String | "upload" | 導入文件保存路徑 |
| type | int | 1 | 導出類型 1 是文本 2 是圖片,3 是函數,10 是數字 默認是文本 |
| width | double | 10 | 列寬 |
| height | double | 10 | 列高,后期打算統一使用@ExcelTarget的height,這個會被廢棄,注意 |
| isStatistics | boolean | fasle | 自動統計數據,在追加一行統計,把所有數據都和輸出這個處理會吞沒異常,請注意這一點 |
| isHyperlink | boolean | false | 超鏈接,如果是需要實現接口返回對象 |
| isImportField | boolean | true | 校驗字段,看看這個字段是不是導入的Excel中有,如果沒有說明是錯誤的Excel,讀取失敗,支持name_id |
| exportFormat | String | "" | 導出的時間格式,以這個是否為空來判斷是否需要格式化日期 |
| importFormat | String | "" | 導入的時間格式,以這個是否為空來判斷是否需要格式化日期 |
| format | String | "" | 時間格式,相當于同時設置了exportFormat 和 importFormat |
| databaseFormat | String | "yyyyMMddHHmmss" | 導出時間設置,如果字段是Date類型則不需要設置 數據庫如果是string 類型,這個需要設置這個數據庫格式,用以轉換時間格式輸出 |
| numFormat | String | "" | 數字格式化,參數是Pattern,使用的對象是DecimalFormat |
| imageType | int | 1 | 導出類型 1 從file讀取 2 是從數據庫中讀取 默認是文件 同樣導入也是一樣的 |
| suffix | String | "" | 文字后綴,如% 90 變成90% |
| isWrap | boolean | true | 是否換行 即支持\n |
| mergeRely | int[] | {} | 合并單元格依賴關系,比如第二列合并是基于第一列 則{1}就可以了 |
| mergeVertical | boolean | fasle | 縱向合并內容相同的單元格 |
導入導出公用方法
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,boolean isCreateHeader, HttpServletResponse response){ ExportParams exportParams = new ExportParams(title, sheetName); exportParams.setCreateHeadRows(isCreateHeader); defaultExport(list, pojoClass, fileName, response, exportParams); } public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){ defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); } public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){ defaultExport(list, fileName, response); } private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) { Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list); if (workbook != null); downLoadExcel(fileName, response, workbook); } private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { throw new NormalException(e.getMessage()); } } private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); if (workbook != null); downLoadExcel(fileName, response, workbook); } public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){ if (StringUtils.isBlank(filePath)){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); }catch (NoSuchElementException e){ throw new NormalException("模板不能為空"); } catch (Exception e) { e.printStackTrace(); throw new NormalException(e.getMessage()); } return list; } public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){ if (file == null){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); }catch (NoSuchElementException e){ throw new NormalException("excel文件不能為空"); } catch (Exception e) { throw new NormalException(e.getMessage()); } return list; }對的,沒看錯,這就可以導出導入了,看起來代碼挺多,其實是提供了多個導入導出方法而已
測試
導出結果
導出結果測試導入
導出結果再添加一行,執行,輸出導入數據行數
作者:小塵哥
鏈接:https://www.jianshu.com/p/5d67fb720ece
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。
總結
以上是生活随笔為你收集整理的【springboot+easypoi】一行代码搞定excel导入导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: React开发(178):ant des
- 下一篇: TypeScript基础入门 - 接口