<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Boot + EasyExcel 導(dǎo)入導(dǎo)出,好用到爆!

          共 2140字,需瀏覽 5分鐘

           ·

          2021-10-25 23:23



          來(lái)源:www.jianshu.com/p/4e6aa6342b33

          EasyExcel是阿里巴巴開(kāi)源poi插件之一,主要解決了poi框架使用復(fù)雜,sax解析模式不容易操作,數(shù)據(jù)量大起來(lái)容易OOM,解決了POI并發(fā)造成的報(bào)錯(cuò)。主要解決方式:通過(guò)解壓文件的方式加載,一行一行的加載,并且拋棄樣式字體等不重要的數(shù)據(jù),降低內(nèi)存的占用。

          EasyExcel優(yōu)勢(shì)

          • 注解式自定義操作。
          • 輸入輸出簡(jiǎn)單,提供輸入輸出過(guò)程的接口
          • 支持一定程度的單元格合并等靈活化操作

          二、常用注解

          • @ExcelProperty 指定當(dāng)前字段對(duì)應(yīng)excel中的那一列。可以根據(jù)名字或者Index去匹配。當(dāng)然也可以不寫(xiě),默認(rèn)第一個(gè)字段就是index=0,以此類(lèi)推。千萬(wàn)注意,要么全部不寫(xiě),要么全部用index,要么全部用名字去匹配。千萬(wàn)別三個(gè)混著用,除非你非常了解源代碼中三個(gè)混著用怎么去排序的。
          • @ExcelIgnore 默認(rèn)所有字段都會(huì)和excel去匹配,加了這個(gè)注解會(huì)忽略該字段
          • @DateTimeFormat 日期轉(zhuǎn)換,用String去接收excel日期格式的數(shù)據(jù)會(huì)調(diào)用這個(gè)注解。里面的value參照java.text.SimpleDateFormat
          • @NumberFormat 數(shù)字轉(zhuǎn)換,用String去接收excel數(shù)字格式的數(shù)據(jù)會(huì)調(diào)用這個(gè)注解。里面的value參照java.text.DecimalFormat
          • @ExcelIgnoreUnannotated默認(rèn)不加ExcelProperty 的注解的都會(huì)參與讀寫(xiě),加了不會(huì)參與

          三、依賴(lài)

          ??
          <dependency>
          ???<groupId>com.alibabagroupId>
          ???<artifactId>easyexcelartifactId>
          ???<version>2.1.4version>
          dependency>
          ????
          <dependency>
          ???<groupId>javax.servletgroupId>
          ???<artifactId>javax.servlet-apiartifactId>
          ???<version>4.0.1version>
          ???<scope>providedscope>
          dependency>
          <dependency>
          ???<groupId>com.alibabagroupId>
          ???<artifactId>fastjsonartifactId>
          ???<version>1.2.47version>
          dependency>

          四、監(jiān)聽(tīng)

          /**
          ?*?EasyExcel?導(dǎo)入監(jiān)聽(tīng)
          ?*/

          public?class?ExcelListener?extends?AnalysisEventListener?{
          ????//可以通過(guò)實(shí)例獲取該值
          ????private?List?datas?=?new?ArrayList();

          ????@Override
          ????public?void?invoke(Object?o,?AnalysisContext?analysisContext)?{
          ????????datas.add(o);//數(shù)據(jù)存儲(chǔ)到list,供批量處理,或后續(xù)自己業(yè)務(wù)邏輯處理。
          ????????doSomething(o);//根據(jù)自己業(yè)務(wù)做處理
          ????}

          ????private?void?doSomething(Object?object)?{
          ????????//1、入庫(kù)調(diào)用接口
          ????}

          ????public?List?getDatas()?{
          ????????return?datas;
          ????}

          ????public?void?setDatas(List?datas)?{
          ????????this.datas?=?datas;
          ????}

          ????@Override
          ????public?void?doAfterAllAnalysed(AnalysisContext?analysisContext)?{
          ????????//?datas.clear();//解析結(jié)束銷(xiāo)毀不用的資源
          ????}
          }

          五、接口導(dǎo)入Excel

          try?{
          ????????????//獲取文件名
          ????????????String?filename?=?file.getOriginalFilename();
          ????????????//獲取文件流
          ????????????InputStream?inputStream?=?file.getInputStream();
          ????????????//實(shí)例化實(shí)現(xiàn)了AnalysisEventListener接口的類(lèi)
          ????????????ExcelListener?listener?=?new?ExcelListener();
          ????????????//傳入?yún)?shù)
          ????????????ExcelReader?excelReader?=?new?ExcelReader(inputStream,?ExcelTypeEnum.XLS,?null,?listener);
          ????????????//讀取信息
          ????????????excelReader.read(new?Sheet(1,?0,?Test.class));
          ????????????//獲取數(shù)據(jù)
          ????????????List?list?=?listener.getDatas();
          ????????????if?(list.size()?>?1)?{
          ????????????????for?(int?i?=?0;?i?????????????????????Testobj?=?(Test)?list.get(i);
          ????????????????????JSONObject?jo?=?new?JSONObject();
          ????????????????}
          ????????????}
          ????????}?catch?(Exception?e)?{
          ????????????System.out.println(e.getMessage());
          ????????}

          六、接口導(dǎo)出Excel (HttpServletResponse response, HttpServletRequest request)

          try?{
          ????String?filenames?=?"111111";
          ????String?userAgent?=?request.getHeader("User-Agent");
          ????if?(userAgent.contains("MSIE")?||?userAgent.contains("Trident"))?{
          ????????filenames?=?URLEncoder.encode(filenames,?"UTF-8");
          ????}?else?{
          ????????filenames?=?new?String(filenames.getBytes("UTF-8"),?"ISO-8859-1");
          ????}
          ????response.setContentType("application/vnd.ms-exce");
          ????response.setCharacterEncoding("utf-8");
          ????response.addHeader("Content-Disposition",?"filename="?+?filenames?+?".xlsx");
          ????EasyExcel.write(response.getOutputStream(),?Test.class).sheet("sheet").doWrite(testList);
          }?catch?(Exception?e)?{
          }

          七、本地導(dǎo)入、本地導(dǎo)出

          List?testList?=?new?ArrayList<>();
          try?{
          ????String?strUrl?=?"C:\\Users\\Administrator\\Desktop\\json.xlsx";
          ????File?multipartFile?=?new?File(strUrl);
          ????InputStream?inputStream?=?new?FileInputStream(multipartFile);
          ????//實(shí)例化實(shí)現(xiàn)了AnalysisEventListener接口的類(lèi)
          ????ExcelListener?listener?=?new?ExcelListener();
          ????//傳入?yún)?shù)
          ????ExcelReader?excelReader?=?new?ExcelReader(inputStream,?ExcelTypeEnum.XLS,?null,?listener);
          ????//讀取信息
          ????excelReader.read(new?Sheet(1,?0,?Test.class));
          ????//獲取數(shù)據(jù)
          ????List?list?=?listener.getDatas();
          ????if?(list.size()?>?1)?{
          ????????for?(int?i?=?0;?i?????????????Testobj?=?(Test)?list.get(i);
          ????????}
          ????}
          }?catch?(Exception?e)?{
          ????System.out.println(e.getMessage());
          }
          try?{
          ????String?strUrl?=?"C:\\Users\\Administrator\\Desktop\\json"+System.currentTimeMillis()+".xlsx";
          ????EasyExcel.write(strUrl,Test.class).sheet("sheet").doWrite(testList);
          }?catch?(Exception?e)?{
          }

          以上就是EasyExcel的基礎(chǔ)使用過(guò)程,歡迎點(diǎn)贊關(guān)注交流。


          END


          推薦閱讀

          一鍵生成Springboot & Vue項(xiàng)目!【畢設(shè)神器】

          Java可視化編程工具系列(一)


          順便給大家推薦一個(gè)GitHub項(xiàng)目,這個(gè) GitHub 整理了上千本常用技術(shù)PDF,絕大部分核心的技術(shù)書(shū)籍都可以在這里找到,

          GitHub地址:https://github.com/javadevbooks/books

          Gitee地址:https://gitee.com/javadevbooks/books

          電子書(shū)已經(jīng)更新好了,你們需要的可以自行下載了,記得點(diǎn)一個(gè)star,持續(xù)更新中..



          瀏覽 43
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                    <th id="afajh"><progress id="afajh"></progress></th>
                    亚洲激情黄色视频 | 天堂AV中文在线 | 最新欧美日韩中文 | 欧美一区二区视频在线77777 | 天天操天天干天天爽 |