码迷,mamicode.com
首页 > 其他好文 > 详细

EasyExcel读取excel文件

时间:2020-12-11 12:26:24      阅读:4      评论:0      收藏:0      [点我收藏+]

标签:ati   存储   highlight   fas   close   json   ack   metadata   unit   

1、导入jar

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

 2、DEMO

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import org.junit.Test;

import java.io.*

public class ExcelExcutor {

    @Test
    public void stripeDataRead() throws IOException {

        File file = new File("/Users/Downloads/js_20201207.js");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        String fileName = "/Users/didi/Downloads/20201207.csv";
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(fileName, StripeData.class, new StripeDataListener(fileOutputStream)).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
        } finally {
            if (excelReader != null) {
                excelReader.finish();
            }
            fileOutputStream.close();
        }
    }
}

 

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class StripeData {
    @ExcelProperty(value = "_id", index = 0)
    private String _id;
    private String created;
    private String card_exp_month;
    private String card_exp_year;
    private String card_bin;
    private String card_last_4;
    private String amount;
    private String currency;
    private String card_country;
    private String card_issuer;
    private String card_funding;
    private String card_network;
    private String reference;
    private String partner;

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public String getCard_exp_month() {
        return card_exp_month;
    }

    public void setCard_exp_month(String card_exp_month) {
        this.card_exp_month = card_exp_month;
    }

    public String getCard_exp_year() {
        return card_exp_year;
    }

    public void setCard_exp_year(String card_exp_year) {
        this.card_exp_year = card_exp_year;
    }

    public String getCard_bin() {
        return card_bin;
    }

    public void setCard_bin(String card_bin) {
        this.card_bin = card_bin;
    }

    public String getCard_last_4() {
        return card_last_4;
    }

    public void setCard_last_4(String card_last_4) {
        this.card_last_4 = card_last_4;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getCard_country() {
        return card_country;
    }

    public void setCard_country(String card_country) {
        this.card_country = card_country;
    }

    public String getCard_issuer() {
        return card_issuer;
    }

    public void setCard_issuer(String card_issuer) {
        this.card_issuer = card_issuer;
    }

    public String getCard_funding() {
        return card_funding;
    }

    public void setCard_funding(String card_funding) {
        this.card_funding = card_funding;
    }

    public String getCard_network() {
        return card_network;
    }

    public void setCard_network(String card_network) {
        this.card_network = card_network;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getPartner() {
        return partner;
    }

    public void setPartner(String partner) {
        this.partner = partner;
    }
}

 

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StripeDataListener extends AnalysisEventListener<StripeData> {
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 5;
    List<StripeData> list = new ArrayList<StripeData>();
    FileOutputStream fileOutputStream;

    public StripeDataListener(FileOutputStream fileOutputStream) {
        this.fileOutputStream = fileOutputStream;
    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data    one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(StripeData data, AnalysisContext context) {
        try {
            StringBuffer sb = new StringBuffer();

            String created = data.getCreated();
            created = created.replace("T", " ").replace("Z", " ");
            if (created.contains(".")) {
                created = created.substring(0, created.indexOf("."));
            }
            data.setCreated(created);
            sb.append("db.stripe_success.insert(").append(JSON.toJSONString(data).replace("_id", "charge_id")).append(");");
            fileOutputStream.write(sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        list.add(data);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据,确保最后遗留的数据也存储到数据库
        saveData();
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
    }
}

 

EasyExcel读取excel文件

标签:ati   存储   highlight   fas   close   json   ack   metadata   unit   

原文地址:https://www.cnblogs.com/use-D/p/14097757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!