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

          exportExcelExcel 快捷導(dǎo)出導(dǎo)入工具

          聯(lián)合創(chuàng)作 · 2023-09-29 22:08

          exportExcel 讓 excel 導(dǎo)出導(dǎo)入更簡(jiǎn)單,告別繁瑣的 excel 導(dǎo)出,實(shí)現(xiàn)自定義導(dǎo)出,模板導(dǎo)出,基于注解導(dǎo)出簡(jiǎn)單方便。

          1、 新建excel導(dǎo)出。

          Excel excel = new Excel(); //新建excel
          ExcelSheet sheet = excel.createSheet(); //新建sheet
          sheet.row(0).cell(2).cellValue("1");    //調(diào)用cellValue(),設(shè)置excel樣式
          sheet.row(1).cell(2).cellValue("2");
          excel.saveExcel("c://test1.xlsx");      //存儲(chǔ)excel

          2、 調(diào)用模板導(dǎo)出。

          Excel excel = new Excel("c://test1.xlsx");
          ExcelSheet sheet = excel.getSheet();         //默認(rèn)獲取第一個(gè)工作簿
          sheet.row(0).cell(2).cellValue("111111111"); //設(shè)置excel value值
          excel.saveExcel("c://test2.xlsx");

          3、 entity list通過(guò)注解導(dǎo)出。

          Student 實(shí)體類

          /**
           * 學(xué)生 excel測(cè)試
           */
          public class Student {
              private static final long serialVersionUID = -4026917215285783232L;
              @ExcelField(title = "姓名",sort = 1)
              private String name;
              @ExcelField(title = "學(xué)校" ,sort = 3)
              private String school;
              @ExcelField(title = "年齡",sort=2)
              private Integer age;
              @ExcelField(title = "入學(xué)時(shí)間",sort = 4)
              private Date joinDate;
              public Student() {}
              //set/get 方法省略。
              .....
          }

          數(shù)據(jù)初始化

          /**
           * 初始化數(shù)據(jù)
           * @return
           */static List  init(){
              List list = new ArrayList();
              Student st1 = new Student("tom","huax",10);
              Student st2 = new Student("tom","huax",10);
              Student st3 = new Student("tom","huax",10);
              list.add(st1);
              list.add(st2);
              list.add(st3);
              //list.forEach(s->System.out.println(s));
              return list;}

          調(diào)用excel導(dǎo)出方法,對(duì)list數(shù)據(jù)循環(huán)導(dǎo)出。

          /**
           * 模板指定位置 list數(shù)據(jù)循環(huán)導(dǎo)出(需要entity注解)
           * @throws IOException
           * @throws NoSuchMethodException
           * @throws IllegalAccessException
           * @throws InvocationTargetException
           */
          @Test
          public void myExcel() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
              Excel excel = new Excel("c://student_temp.xls");
              ExcelSheet sheet = excel.getSheet();
              sheet.setDateList(init(), 2, 0);  //此處2,0位置為row,cell起始位置
              excel.saveExcel("c://student_temp_rs.xlsx");
          }

          模板

          image

          導(dǎo)出的數(shù)據(jù)

          image

          4.基于注解導(dǎo)出excel

          注解導(dǎo)出(無(wú)樣式)

          Excel excel = new Excel();
          ExcelSheet sheet = excel.createSheet();
          sheet.title("學(xué)生統(tǒng)計(jì)表"); //設(shè)置excel title名稱(可不設(shè))
          sheet.header(Student.class).setData(init()); //設(shè)置 data
          excel.saveExcel("c://student_annotation.xlsx");

          效果

          image

          注解導(dǎo)出(自定義樣式)

          Excel excel = new Excel();
          ExcelSheet sheet = excel.createSheet();
          //獲取excel樣式
          Map styles = createStyles(excel.getWorkbook());
          sheet.title("學(xué)生統(tǒng)計(jì)表").cellStyle(styles.get("title"));    //設(shè)置title 以及樣式
          sheet.header(Student.class, styles.get("header"))           //設(shè)置hear 以及樣式
                  .setData(init()).cellStyle(styles.get("data"));     //設(shè)置data 樣式
          excel.saveExcel("c://student_annotation.xlsx");

          效果

          image

          5.excel導(dǎo)入 (測(cè)試導(dǎo)入上圖內(nèi)容)

          直接將excel內(nèi)容導(dǎo)入到 List對(duì)象中

          Excel excel = new Excel("c://student_annotation.xlsx");
          ExcelSheet sheet = excel.getSheet();
          List> list = sheet.getList(1, 0).toMap(); //1,0(為起始位置,從header開(kāi)始算起)核心方法
          list.forEach(map -> System.out.println(map));
          

          打印結(jié)果

          {姓名=tom, 年齡=10, 學(xué)校=huax, 入學(xué)時(shí)間=2017-08-15}
          {姓名=tom, 年齡=10, 學(xué)校=huax, 入學(xué)時(shí)間=2017-08-15}
          {姓名=tom, 年齡=10, 學(xué)校=huax, 入學(xué)時(shí)間=2017-08-15}
          

          考慮到上述數(shù)據(jù)不是我們所需要的,特此增加注解導(dǎo)出的方法。

          List> list = sheet.getList(1, 0).toMap4Annotation(Student.class); 
          

          打印結(jié)果

          {name=tom, age=10, school=huax, joinDate=2017-08-15}
          {name=tom, age=10, school=huax, joinDate=2017-08-15}
          {name=tom, age=10, school=huax, joinDate=2017-08-15}
          

          有可能注解并不能很實(shí)用,在實(shí)際開(kāi)發(fā)中,為此增加了自定義的方式(打印結(jié)果如上)。

          String keyValue = "姓名:name,學(xué)校:school,年齡:age,入學(xué)時(shí)間:joinDate";
          List> list = sheet.getList(1, 0,keyValue).toMap();
          

          6.excel導(dǎo)入映射至對(duì)象

          實(shí)際開(kāi)發(fā)中,更為實(shí)用的是直接映射到對(duì)象里,為此新增了導(dǎo)入對(duì)象的方法。

          List list = sheet.getList(1, 0).toObject(Student.class);
          

          同時(shí)可自定義導(dǎo)入對(duì)象屬性中。

          String keyValue = "姓名:name,學(xué)校:school,年齡:age,入學(xué)時(shí)間:joinDate";
          List list = sheet.getList(1, 0,keyValue).toObject(Student.class);
          

          打印結(jié)果

          Student{name='tom', school='huax', age=10, joinDate=Tue Aug 15 00:00:00 CST 2017}
          Student{name='tom', school='huax', age=10, joinDate=Tue Aug 15 00:00:00 CST 2017}
          Student{name='tom', school='huax', age=10, joinDate=Tue Aug 15 00:00:00 CST 2017}

          7.獲取單條數(shù)據(jù)

          String title = sheet.getRow(0).getCell(0).getCellValue(); //根據(jù)指定位置獲取數(shù)據(jù),統(tǒng)一為String
          System.out.println(title);
          

          打印結(jié)果

          學(xué)生統(tǒng)計(jì)表

          8.excel導(dǎo)出樣式優(yōu)化,簡(jiǎn)化單元格樣式設(shè)置。

          //設(shè)置樣式
          CellStyle cellStyle = ExcelStyle.builder(excel.getWorkbook())
                          .align(HSSFCellStyle.ALIGN_CENTER) //設(shè)置居中
                          .fondFamily("宋體")  //設(shè)置字體
                          .fondSize((short) 12) //設(shè)置字體大小
                          .fondWeight((short) 10) //加粗
                          .border(ExcelStyle.BORDER_TOP, ExcelStyle.BORDER_LEFT, ExcelStyle.BORDER_BOTTOM, ExcelStyle.BORDER_RIGHT) //設(shè)置表格邊框
                          .build();

          9.excel 推薦快速導(dǎo)出

          Excel excel = new Excel();
          ExcelSheet sheet = excel.createSheet();
          sheet.header("姓名,年齡,學(xué)校,日期", 0, 0).cellStyle(ExcelStyle.getCommHeader(excel.getWorkbook()));
          sheet.setDateList(init(), 1, 0).cellStyle(ExcelStyle.getCommData(excel.getWorkbook()));
          excel.saveExcel("c://fast_export_excel.xlsx");
          瀏覽 21
          點(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>
                  自拍激情网址 | 欧美另类视频 | 国产精品久久久久久妇女6080 | 中文字幕在线一页 | 精品免费久久久久 |