国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

java使用EasyExcel導入導出excel

發(fā)布時(shí)間:2021-07-05 18:40 來(lái)源:腳本之家 閱讀:0 作者:迷途者尋影而行 欄目: 開(kāi)發(fā)技術(shù)

目錄

    一、準備工作

    1、導包

    <!-- poi 相關(guān)-->
    <dependency>
    	<groupId>org.apache.poi</groupId>
    	<artifactId>poi</artifactId>
    	<version>3.17</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.poi</groupId>
    	<artifactId>poi-ooxml-schemas</artifactId>
    	<version>3.17</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.poi</groupId>
    	<artifactId>poi-ooxml</artifactId>
    	<version>3.17</version>
    </dependency>
    <!-- esayexcel 2.1.7  -->
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>easyexcel</artifactId>
    	<version>2.1.7</version>
    </dependency>
    

    二、了解注解

    1、常用注解

    2、@ExcelProperty注解

    必要的一個(gè)注解,注解中有三個(gè)參數value,index分別代表列明,列序號
    value和index只能二選一,通常不用設置converter
    1.value 通過(guò)標題文本對應
    2.index 通過(guò)文本行號對應

    @ExcelProperty(value = "編號", index = 0)
    private Long id;
    

    3、@ColumnWith注解

    設置列寬度,只有一個(gè)參數value,value的單位是字符長(cháng)度,最大可以設置255個(gè)字符,因為一個(gè)excel單元格最大可以寫(xiě)入的字符個(gè)數就是255個(gè)字符

    public class ImeiEncrypt {
        @ColumnWidth(value = 255) //excel單個(gè)單元格最大長(cháng)度255
        private String message;
    }
    

    4、@ContentFontStyle注解

    用于設置單元格內容字體格式的注解

    5、@ContentStyle注解

    設置內容格式注解

    6、@HeadFontStyle注解

    用于定制標題字體格式

    7、ExcelIgnore注解

    不將該字段轉換成Excel

    三、編碼

    1、映射實(shí)體類(lèi)----例子

    package com.pingou.admin.bean.param;
    
    import com.alibaba.excel.annotation.ExcelProperty;
    import com.alibaba.excel.annotation.format.DateTimeFormat;
    import com.alibaba.excel.annotation.write.style.ColumnWidth;
    import com.alibaba.excel.annotation.write.style.ContentRowHeight;
    import com.alibaba.excel.annotation.write.style.HeadRowHeight;
    import lombok.Data;
    
    import java.math.BigDecimal;
    import java.util.Date;
    
    @Data
    @ContentRowHeight(35) //文本行高度
    @HeadRowHeight(40)    //標題高度
    @ColumnWidth(40)
    public class OrderExcel {
        //設置excel表頭名稱(chēng)
        @ExcelProperty(value = "編號", index = 0)
        private Long id;
        @DateTimeFormat("yyyy年MM月dd日HH時(shí)mm分ss秒")
        @ExcelProperty(value = "創(chuàng  )建時(shí)間", index = 1)
        private Date createTime;
    }
    

    以上是簡(jiǎn)單的舉例,如果有更多屬性自己逐個(gè)寫(xiě)就好,然后塞進(jìn)該實(shí)體類(lèi)就好~

    2、生成excel

    public void excel() {
            //欲導出excel的數據結果集
            List<OrderExcel> excel = new ArrayList<>();
            //省略 向結果集里插入數據的操作
    
            //UUID生成唯一name
            String name = UUID.randomUUID().toString().replaceAll("-", "") + ".xlsx";
            //實(shí)現excel寫(xiě)的操作
    
            //1 設置寫(xiě)入文件夾地址和excel文件名稱(chēng)
            String filename = "/路徑" + name;
            JSONObject json = new JSONObject();
            try {
                // 2 調用easyexcel里面的方法實(shí)現寫(xiě)操作
                // write方法兩個(gè)參數:第一個(gè)參數文件路徑名稱(chēng),第二個(gè)參數實(shí)體類(lèi)class
                EasyExcel.write(filename, OrderExcel.class).sheet("名字").doWrite(excel);
                //上傳到fastdfs上 不上傳的話(huà)只有本機可以找到,在上面路徑下生成excel  
                File file = new File(filename);
                String path = fastDFSClient.upload(new FileInputStream(file), name, null);
                path = (this.fastdfsDomain + path);
                json.put("url", path);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                new File(filename).delete();
            }
        }
    

    以上,就生成完畢了

    四、結果

    以上就是java使用EasyExcel導入導出excel的詳細內容,更多關(guān)于java 用EasyExcel導入導出excel的資料請關(guān)注腳本之家其它相關(guān)文章!

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

    亚洲中文字幕无码一区| 久久精品国产只有精品96| 自慰无码一区二区三区| 亚洲精品成人片在线观看精品字幕| 精品免费久久久久久久| 三年片在线观看免费大全|