日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【springboot+easypoi】一行代码搞定excel导入导出

發(fā)布時(shí)間:2023/12/9 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【springboot+easypoi】一行代码搞定excel导入导出 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文:https://www.jianshu.com/p/5d67fb720ece

?

?

  • 開(kāi)發(fā)中經(jīng)常會(huì)遇到excel的處理,導(dǎo)入導(dǎo)出解析等等,java中比較流行的用poi,但是每次都要寫大段工具類來(lái)搞定這事兒,此處推薦一個(gè)別人造好的輪子【easypoi】,下面介紹下“輪子”的使用。

pom引入

  • 不再需要其他jar
<dependency><groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency>

編寫實(shí)體類

  • 此處注意必須要有空構(gòu)造函數(shù),否則會(huì)報(bào)錯(cuò)“對(duì)象創(chuàng)建錯(cuò)誤”
  • 關(guān)于注解@Excel,其他還有@ExcelCollection,@ExcelEntity ,@ExcelIgnore,@ExcelTarget等,此處我們用不到,可以去官方查看更多
屬性類型類型說(shuō)明
nameStringnull列名
needMergebooleanfasle縱向合并單元格
orderNumString"0"列的排序,支持name_id
replaceString[]{}值得替換 導(dǎo)出是{a_id,b_id} 導(dǎo)入反過(guò)來(lái)
savePathString"upload"導(dǎo)入文件保存路徑
typeint1導(dǎo)出類型 1 是文本 2 是圖片,3 是函數(shù),10 是數(shù)字 默認(rèn)是文本
widthdouble10列寬
heightdouble10列高,后期打算統(tǒng)一使用@ExcelTarget的height,這個(gè)會(huì)被廢棄,注意
isStatisticsbooleanfasle自動(dòng)統(tǒng)計(jì)數(shù)據(jù),在追加一行統(tǒng)計(jì),把所有數(shù)據(jù)都和輸出這個(gè)處理會(huì)吞沒(méi)異常,請(qǐng)注意這一點(diǎn)
isHyperlinkbooleanfalse超鏈接,如果是需要實(shí)現(xiàn)接口返回對(duì)象
isImportFieldbooleantrue校驗(yàn)字段,看看這個(gè)字段是不是導(dǎo)入的Excel中有,如果沒(méi)有說(shuō)明是錯(cuò)誤的Excel,讀取失敗,支持name_id
exportFormatString""導(dǎo)出的時(shí)間格式,以這個(gè)是否為空來(lái)判斷是否需要格式化日期
importFormatString""導(dǎo)入的時(shí)間格式,以這個(gè)是否為空來(lái)判斷是否需要格式化日期
formatString""時(shí)間格式,相當(dāng)于同時(shí)設(shè)置了exportFormat 和 importFormat
databaseFormatString"yyyyMMddHHmmss"導(dǎo)出時(shí)間設(shè)置,如果字段是Date類型則不需要設(shè)置 數(shù)據(jù)庫(kù)如果是string 類型,這個(gè)需要設(shè)置這個(gè)數(shù)據(jù)庫(kù)格式,用以轉(zhuǎn)換時(shí)間格式輸出
numFormatString""數(shù)字格式化,參數(shù)是Pattern,使用的對(duì)象是DecimalFormat
imageTypeint1導(dǎo)出類型 1 從file讀取 2 是從數(shù)據(jù)庫(kù)中讀取 默認(rèn)是文件 同樣導(dǎo)入也是一樣的
suffixString""文字后綴,如% 90 變成90%
isWrapbooleantrue是否換行 即支持\n
mergeRelyint[]{}合并單元格依賴關(guān)系,比如第二列合并是基于第一列 則{1}就可以了
mergeVerticalbooleanfasle縱向合并內(nèi)容相同的單元格
import cn.afterturn.easypoi.excel.annotation.Excel;import java.util.Date;public class Person { @Excel(name = "姓名", orderNum = "0") private String name; @Excel(name = "性別", replace = {"男_1", "女_2"}, orderNum = "1") private String sex; @Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2") private Date birthday; public Person(String name, String sex, Date birthday) { this.name = name; this.sex = sex; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }

導(dǎo)入導(dǎo)出公用方法

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

對(duì)的,沒(méi)看錯(cuò),這就可以導(dǎo)出導(dǎo)入了,看起來(lái)代碼挺多,其實(shí)是提供了多個(gè)導(dǎo)入導(dǎo)出方法而已

測(cè)試

@RequestMapping("export")public void export(HttpServletResponse response){ //模擬從數(shù)據(jù)庫(kù)獲取需要導(dǎo)出的數(shù)據(jù) List<Person> personList = new ArrayList<>(); Person person1 = new Person("路飛","1",new Date()); Person person2 = new Person("娜美","2", DateUtils.addDate(new Date(),3)); Person person3 = new Person("索隆","1", DateUtils.addDate(new Date(),10)); Person person4 = new Person("小貍貓","1", DateUtils.addDate(new Date(),-10)); personList.add(person1); personList.add(person2); personList.add(person3); personList.add(person4); //導(dǎo)出操作 FileUtil.exportExcel(personList,"花名冊(cè)","草帽一伙",Person.class,"海賊王.xls",response); } @RequestMapping("importExcel") public void importExcel(){ String filePath = "F:\\海賊王.xls"; //解析excel, List<Person> personList = FileUtil.importExcel(filePath,1,1,Person.class); //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)導(dǎo)入 System.out.println("導(dǎo)入數(shù)據(jù)一共【"+personList.size()+"】行"); //TODO 保存數(shù)據(jù)庫(kù) }

導(dǎo)出結(jié)果

導(dǎo)出結(jié)果

測(cè)試導(dǎo)入

導(dǎo)出結(jié)果再添加一行,執(zhí)行,輸出導(dǎo)入數(shù)據(jù)行數(shù)




作者:小塵哥
鏈接:https://www.jianshu.com/p/5d67fb720ece
來(lái)源:簡(jiǎn)書
簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。

總結(jié)

以上是生活随笔為你收集整理的【springboot+easypoi】一行代码搞定excel导入导出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。