<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 導入導出,好用到爆,可以扔掉 POI 了!

          共 2017字,需瀏覽 5分鐘

           ·

          2022-01-02 15:10

          EasyExcel

          ?

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


          EasyExcel優(yōu)勢


          • 注解式自定義操作。

          • 輸入輸出簡單,提供輸入輸出過程的接口

          • 支持一定程度的單元格合并等靈活化操作


          ?

          2

          常用注解


          • @ExcelProperty 指定當前字段對應excel中的那一列??梢愿鶕?jù)名字或者Index去匹配。當然也可以不寫,默認第一個字段就是index=0,以此類推。千萬注意,要么全部不寫,要么全部用index,要么全部用名字去匹配。千萬別三個混著用,除非你非常了解源代碼中三個混著用怎么去排序的。

          • @ExcelIgnore 默認所有字段都會和excel去匹配,加了這個注解會忽略該字段

          • @DateTimeFormat 日期轉(zhuǎn)換,用String去接收excel日期格式的數(shù)據(jù)會調(diào)用這個注解。里面的value參照java.text.SimpleDateFormat

          • @NumberFormat 數(shù)字轉(zhuǎn)換,用String去接收excel數(shù)字格式的數(shù)據(jù)會調(diào)用這個注解。里面的value參照java.text.DecimalFormat

          • @ExcelIgnoreUnannotated默認不加ExcelProperty 的注解的都會參與讀寫,加了不會參與



          3

          依賴



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



          4

          監(jiān)聽


          /**
          ?* EasyExcel 導入監(jiān)聽
          ?*/

          public?class?ExcelListener?extends?AnalysisEventListener {
          ????//可以通過實例獲取該值
          ????private?List<Object> datas =?new?ArrayList<Object>();

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

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

          ????public?List<Object> getDatas() {
          ????????return?datas;
          ????}

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

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



          5

          接口導入Excel


          try?{
          ????//獲取文件名
          ????String filename = file.getOriginalFilename();
          ????//獲取文件流
          ????InputStream inputStream = file.getInputStream();
          ????//實例化實現(xiàn)了AnalysisEventListener接口的類
          ????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 list.size(); i++) {
          ????????????Testobj = (Test)?list.get(i);
          ????????????JSONObject jo =?new?JSONObject();
          ????????}
          ????}
          }?catch?(Exception?e) {
          ????System.out.println(e.getMessage());
          }



          6

          接口導出Excel?


          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) {
          }



          7

          本地導入、本地導出


          List testList =?new?ArrayList<>();
          try?{
          ????String strUrl =?"C:\\Users\\Administrator\\Desktop\\json.xlsx";
          ????File multipartFile =?new?File(strUrl);
          ????InputStream inputStream =?new?FileInputStream(multipartFile);
          ????//實例化實現(xiàn)了AnalysisEventListener接口的類
          ????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 list.size(); 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的基礎使用過程,大家可以自己嘗試下。


          來源:jianshu.com/p/4e6aa6342b33


          < END >
          瀏覽 27
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                    国产无码A片 | 小菊花天天要人干 | 91三级片在线 | 奇米影视7777狠狠狠狠视频 | 日本中文字幕在线 |