【Easyexcel】java導入導出超大數據量的xlsx文件 解決方法

 

解決方法:html

使用easyexcel解決超大數據量的導入導出xlsx文件java

easyexcel最大支持行數 1048576。git

 

 

 

 

 

官網地址:github

https://alibaba-easyexcel.github.io/dom

 

GitHub地址:ide

https://github.com/alibaba/easyexcel大數據

 

使用示例:ui

Java數據類【重點是屬性上的註解】:this

package com.dmall.proengine.domain.man.partner.bean;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.dmall.proengine.domain.common.enums.PromotionStatusEnum;
import com.dmall.proengine.sdk.enums.PromotionSubTypeEnum;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: SXD
 * @Description:
 * @Date: create in 2019/9/26 11:43
 */
@ColumnWidth(25)
public class ProSkuSearchInfoDisplay {

    /**
     * 促銷ID
     */
    @ExcelProperty(value = "促銷編碼",index = 6)
    private String proId;

    /**
     * 參與類型
     */
    @ExcelIgnore
    private Integer itemType;

    /**
     * 商品sku 或  商品mc
     * DB查詢出來的值
     */
    @ExcelIgnore
    private String itemCode;

    /**
     * 商品sku
     * 最終結果值
     */
    @ExcelProperty(value = "商品sku",index = 0)
    private Long sku;

    /**
     * 物料編碼
     */
    @ExcelProperty(value = "物料編碼",index = 1)
    private String matnrCode;

    /**
     * 商品名稱
     */
    @ExcelProperty(value = "商品名稱",index = 2)
    private String skuName;

    /**
     * 國條碼
     */
    @ExcelProperty(value = "國條碼",index = 3)
    private String barCode;
    /**
     * 商品MC
     * 最終結果值
     */
    @ExcelProperty(value = "商品MC",index = 4)
    private String skuMc;

    /**
     * 商品MC Name
     */
    @ExcelIgnore
    private String skuMcName;

    /**
     * 促銷檔期
     */
    @ExcelProperty(value = "促銷檔期",index = 5)
    private String  proSchedule;

    /**
     * 促銷編碼
     */
    @ExcelIgnore
    private String proCode;

    /**
     * 促銷名稱
     */
    @ExcelProperty(value = "促銷名稱",index =7)
    private String proName;

    /**
     * 促銷詳情
     */
    @ColumnWidth(50)
    @ExcelProperty(value = "促銷詳情",index =8)
    private String proDetail;

    /**
     * 促銷類型
     */
    @ExcelIgnore
    private Integer proType;

    /**
     * 促銷子類型
     */
    @ExcelIgnore
    private Integer proSubType;

    /**
     * 促銷類型名稱
     */
    @ExcelProperty(value = "促銷類型",index =9)
    private String proTypeName;

    /**
     * 促銷售價  單位:分
     */
    @ExcelProperty(value = "促銷售價",index =10)
    private Double proPrice;

    /**
     * 促銷折扣值
     * 僅單品促銷實際應用本字段
     * 單品直降  101   skuPrice-rewardValue = proPrice
     * 單品特價  102   proPrice =  rewardValue
     * 單品折扣  103   skuPrice*(rewardValue/10000) =  proPrice
     */
    @ExcelIgnore
    private Long rewardValue;

    /**
     * 商品原價 單位:分
     */
    @ExcelProperty(value = "商品原價",index =11)
    private Double skuPrice;

    /**
     * 促銷狀態
     */
    @ExcelIgnore
    private Integer proStatus;

    /**
     * 促銷狀態名稱
     */
    @ExcelProperty(value = "促銷狀態",index =12)
    private String proStatusName;

    /**
     * PO訂單號  暫無
     * 採銷系統相關
     */
    @ExcelIgnore
    private String poOrderCode;

    /**
     * STO訂單號  暫無
     * 採銷系統相關
     */
    @ExcelIgnore
    private String stoOrderCode;

    /**
     * 預期到店日  暫無
     * 採銷系統相關
     */
    @ExcelIgnore
    private Date expectedDate;

    /**
     * 促銷開始時間
     */
    @ExcelProperty(value = "促銷開始時間",index =13)
    private Date proStartTime;

    /**
     * 促銷結束時間
     */
    @ExcelProperty(value = "促銷結束時間",index =14)
    private Date proEndTime;


    /**
     * 單品 三種折扣 計算促銷價格
     */
    public void calcuProPrice(){

        if (skuPrice != null){
            if (proSubType == PromotionSubTypeEnum.SINGLE_CUT_PRICE.getValue()){
                proPrice = skuPrice-rewardValue;
            }
            if (proSubType == PromotionSubTypeEnum.SINGLE_SPECIAL_PRICE.getValue()){
                proPrice = (double)rewardValue;
            }
            if (proSubType == PromotionSubTypeEnum.SINGLE_REBATE.getValue()){
                proPrice =  skuPrice - BigDecimal.valueOf(skuPrice).subtract(BigDecimal.valueOf(rewardValue * skuPrice).divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP)).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
            }
        }

    }

    /**
     * 組裝最終展現數據
     */
    public void assembleParams(){
        proTypeName = PromotionSubTypeEnum.getDesc(proSubType);
        proStatusName = PromotionStatusEnum.getDesc(proStatus);
        proPrice = proPrice != null ? proPrice/(double)100 : null;
        skuPrice = skuPrice != null ? skuPrice/(double)100 : null;
    }


    public String getBarCode() {
        return barCode;
    }

    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }

    public String getProTypeName() {
        return proTypeName;
    }

    public void setProTypeName(String proTypeName) {
        this.proTypeName = proTypeName;
    }

    public String getProStatusName() {
        return proStatusName;
    }

    public void setProStatusName(String proStatusName) {
        this.proStatusName = proStatusName;
    }

    public Date getProStartTime() {
        return proStartTime;
    }

    public void setProStartTime(Date proStartTime) {
        this.proStartTime = proStartTime;
    }

    public Date getProEndTime() {
        return proEndTime;
    }

    public void setProEndTime(Date proEndTime) {
        this.proEndTime = proEndTime;
    }

    public Long getRewardValue() {
        return rewardValue;
    }

    public void setRewardValue(Long rewardValue) {
        this.rewardValue = rewardValue;
    }

    public String getProId() {
        return proId;
    }

    public void setProId(String proId) {
        this.proId = proId;
    }

    public Long getSku() {
        if (sku == null){
            setSkuFromItemCode();
        }
        return sku;
    }

    public void setSkuFromItemCode(){
        setSku(Long.parseLong(itemCode));
    }

    public void setSku(Long sku) {
        this.sku = sku;
    }

    public Integer getItemType() {
        return itemType;
    }

    public void setItemType(Integer itemType) {
        this.itemType = itemType;
    }

    public String getItemCode() {
        return itemCode;
    }

    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }

    public String getMatnrCode() {
        return matnrCode;
    }

    public void setMatnrCode(String matnrCode) {
        this.matnrCode = matnrCode;
    }

    public String getSkuName() {
        return skuName;
    }

    public void setSkuName(String skuName) {
        this.skuName = skuName;
    }

    public String getSkuMc() {
        return skuMc;
    }

    public void setSkuMc(String skuMc) {
        this.skuMc = skuMc;
    }

    public String getSkuMcName() {
        return skuMcName;
    }

    public void setSkuMcName(String skuMcName) {
        this.skuMcName = skuMcName;
    }

    public String getProSchedule() {
        return proSchedule;
    }

    public void setProSchedule(String proSchedule) {
        this.proSchedule = proSchedule;
    }

    public String getProCode() {
        return proCode;
    }

    public void setProCode(String proCode) {
        this.proCode = proCode;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public String getProDetail() {
        return proDetail;
    }

    public void setProDetail(String proDetail) {
        this.proDetail = proDetail;
    }

    public Integer getProType() {
        return proType;
    }

    public void setProType(Integer proType) {
        this.proType = proType;
    }

    public Integer getProSubType() {
        return proSubType;
    }

    public void setProSubType(Integer proSubType) {
        this.proSubType = proSubType;
    }

    public Double getProPrice() {
        return proPrice;
    }

    public void setProPrice(Double proPrice) {
        this.proPrice = proPrice;
    }

    public Double getSkuPrice() {
        return skuPrice;
    }

    public void setSkuPrice(Double skuPrice) {
        this.skuPrice = skuPrice;
    }

    public Integer getProStatus() {
        return proStatus;
    }

    public void setProStatus(Integer proStatus) {
        this.proStatus = proStatus;
    }

    public String getPoOrderCode() {
        return poOrderCode;
    }

    public void setPoOrderCode(String poOrderCode) {
        this.poOrderCode = poOrderCode;
    }

    public String getStoOrderCode() {
        return stoOrderCode;
    }

    public void setStoOrderCode(String stoOrderCode) {
        this.stoOrderCode = stoOrderCode;
    }

    public Date getExpectedDate() {
        return expectedDate;
    }

    public void setExpectedDate(Date expectedDate) {
        this.expectedDate = expectedDate;
    }
}
View Code

 

生成xlsx文件:編碼

private static final String UPLOAD_TEMP_FILE_NAME = "導出xlsx文件-%s.xlsx";


 private File createXlsxFile2(List<ProSkuSearchInfoDisplay> list,String recordKey){
        String filePath = getFilePath(recordKey);

        ExcelWriter excelWriter = EasyExcel.write(filePath, ProSkuSearchInfoDisplay.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("促銷商品數據").build();
        excelWriter.write(list, writeSheet);
        /// 千萬別忘記finish 會幫忙關閉流
        excelWriter.finish();

        return new File(filePath);
    }



 /**
     * 獲取臨時文件路徑
     * @return
     */
    private String getFilePath(String recordKey){
        String path = ProExportSkuDataJob.class.getResource("/").getPath()+String.format(UPLOAD_TEMP_FILE_NAME, recordKey.substring(recordKey.lastIndexOf(":")+1));
        DpeLogUtil.info("dpePartner#ProExportSkuDataJob createFilePath={"+path+"}");
        return path;
    }

 

 

 

使用過程當中報異常和處理的方法:

http://www.javashuo.com/article/p-kixuulda-kn.html

相關文章
相關標籤/搜索