阿里 easyexcel导入

pom依赖

		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.1</version>
        </dependency>

监听对象, 用于接收读取excel 中读取数据

package com.exx.dzj.util.excel;

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yangyun
 * @create 2019-03-19-10:15
 */
public class ExcelListener extends AnalysisEventListener {

    /**
     * @description 用于保存数据
     * @author yangyun
     * @date 2019/3/27 0027
     * @param null
     * @return
     */
    private List<Object> datas = Collections.synchronizedList(new ArrayList<>());

    /**
     * @description
     * @author yangyun
     * @date 2019/3/19 0019
     * @param o 读取的每行数据
     * @param analysisContext
     * @return void
     */
    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        datas.add(o);
    }

    /**
     * @description excel 读取完之后的操作
     * @author yangyun
     * @date 2019/3/27 0027
     * @param analysisContext
     * @return void
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        analysisContext.
    }

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

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



package com.exx.dzj.util.excel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.exx.dzj.constant.CommonConstant;
import com.exx.dzj.excepte.ErpException;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @description alibaba easyexcel 解析 excel 解析单个excel
 * @author yangyun
 * @date 2019/3/19 0019
 * @return
 */
public class ExcelUtil {

    /**
     * @description
     * @author yangyun
     * @date 2019/3/19 0019
     * @param excel 上传excel 文件
     * @param rowModel 对应实体对象
     * @param sheetNo 解析开始行, 有表头的情况从第一行开始
     * @return java.util.List<java.lang.Object>
     */
    public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo) throws IOException {
        return readExcel(excel, rowModel, sheetNo, CommonConstant.DEFAULT_VALUE_ONE);
    }

    public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo, int headLineNum) throws IOException {
        ExcelListener excelListener = new ExcelListener();
        ExcelReader reader = getReader(excel, excelListener);
        if (reader == null) {
            return null;
        }
        reader.read(new Sheet(sheetNo, headLineNum, rowModel.getClass()));
        return excelListener.getDatas();
    }

    public static ExcelReader getReader(MultipartFile excel, ExcelListener excelListener) throws IOException{
        String fileName = excel.getOriginalFilename();
        if (fileName == null || !(fileName.toLowerCase().endsWith(".xls") || fileName.toLowerCase().endsWith(".xlsx") || fileName.toLowerCase().endsWith(".xlsm"))){
            throw new ErpException(CommonConstant.FAIL_CODE, "文件格式错误");
        }

        InputStream is = new BufferedInputStream(excel.getInputStream());

        return new ExcelReader(is, null, excelListener, false);
    }
}

上面两个类就是主要解析和接收数据的类, 还需要一个model 与excel 的没列表格映射, 获取每行数据, 该映射对象需要继承 BaseRowModel

package com.exx.dzj.model;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;

import java.math.BigDecimal;

/**
 * @author yangyun
 * @create 2019-03-27-15:38
 */
@Data
public class StockModel extends BaseRowModel {
    @ExcelProperty(value = "存货编号", index = 0)
    private String stockCode;

    @ExcelProperty(value = "存货性质", index = 1) //
    private String nature;

    @ExcelProperty(value = "存货名称", index = 2)
    private String stockName;

    @ExcelProperty(value = "规格型号", index = 3)
    private String specificateType;

    @ExcelProperty(value = "存货类别", index = 4)
    private String stockClass;

    @ExcelProperty(value = "默认存货地点", index = 5)
    private String stockAddress;

    @ExcelProperty(value = "计量单位", index = 6)
    private String meterUnit;

    @ExcelProperty(value = "条形码", index = 7)
    private String barCode;

    @ExcelProperty(value = "标准买价", index = 8)
    private BigDecimal standardBuyPrice;

    @ExcelProperty(value = "标准卖价", index = 9)
    private BigDecimal standardSellPrice;

    @ExcelProperty(value = "最高采购限价", index = 10)
    private BigDecimal maxPurchasePrice;

    @ExcelProperty(value = "最低销售限价", index = 11)
    private BigDecimal minSellPrice;

    @ExcelProperty(value = "最低存量", index = 12)
    private Integer minInventory;

    @ExcelProperty(value = "状态", index = 12) // 1 使用中 0 禁用
    private String status;
}

在这里插入图片描述
代码已完, 那么问题来了
在这里插入图片描述
由于下面列的格式导致还有 4 个 byte 没有被读取
在这里插入图片描述
将文件另存为启用宏的 excel 工作簿,
在这里插入图片描述
在这里插入图片描述
将后缀名一改就ok了, 或者在代码中允许这种格式的excel上传
后面又遇到这个问题
SQL state [HY000]; error code [1366]; Incorrect string value: ‘\xF0\xA3\x87\x88\xE5\xA8…’ for column ‘cust_name’ at row 13078; nested exception is java.sql.SQLException: Incorrect string value: ‘\xF0\xA3\x87\x88\xE5\xA8…’ for column 意思就是说从 excel 中导入数据, 但是由于 mysql
中编码设置不能兼容, 所以出错了.
在这里插入图片描述
server 中字符编码为 utf8 , 虽然开始查看表字段编码设置的时候为 utf8mb4, 但还是不能正常导入数据, 然后就是修改 mysql 的my.ini 文件中默认编码设置了
在这里插入图片描述
记得修改完要重启mysql
在这里插入图片描述
然后就可以正常导入了, 还要注意一点的就是, 因为我是从别的系统导入数据进自己家数据库, 所以字段长度设计的时候除了小小的问题, 一般出这个错一眼就能看出来的;
如果有同学遇到其他问题欢迎一起进步!!!

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值