java用easyexcel导入导出excel

easyexcel导出excel模板

pom文件添加jar依赖

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

导出excle

    @GetMapping("export")
    public void export(CivilizedLuckyDrawQuery query, HttpServletResponse response) {
        DataResult<DrawVO> luckyDrawVODataResult = luckyDrawService.queryCivilizedList(query);
        List<DrawVO> data = luckyDrawVODataResult.getData();
        try {
            String fileName = "excel命名_"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            String sheetName = "sheet命名";
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
            EasyExcel.write(response.getOutputStream(), CivilizedLuckyDrawVO.class).sheet(sheetName).doWrite(data);
        } catch (IOException e) {
            log.error("导出异常,e: {}", e);
        }
    }

导入excel

	/**
     * 解析excel取得数据
     *
     * @param tempFile
     * @param suffix_flex
     * @return
     */
    public List<List<String>> getDatasByDealExcel(File tempFile, String suffix_flex) {
        InputStream in = null;
        try {
            in = new FileInputStream(tempFile);
            ExcelListener listener = new ExcelListener();
            ExcelTypeEnum type = suffix_flex.equals(".xls") ? ExcelTypeEnum.XLS : ExcelTypeEnum.XLSX;
            ExcelReader excelReader = new ExcelReader(in, type, null, listener);
            excelReader.read();
            //线面的获取也是正确的
            //Sheet sheet = new Sheet(1, 1);
            // List<Object> list = EasyExcelFactory.read(in, sheet);
            List<List<String>> datas = listener.getDatas();

            return datas;
        } catch (Exception e) {

        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                log.error("excel文件读取失败, 失败原因:{}", e);
            }
        }
        return null;
    }

ExcelListener代码展示;

@Component
public class ExcelListener extends AnalysisEventListener {

    public List<List<String>> datas = new ArrayList<>();

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        List<String> stringList= (List<String>) o;
        System.out.println("当前sheet"+analysisContext.getCurrentSheet().getSheetNo()+ " 当前行:" + analysisContext.getCurrentRowNum()
                + " data:" + stringList.get(0));
        datas.add(stringList);//数据存储到list,供批量处理,或后续自己业务逻辑处理。
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public List<List<String>> getDatas() { return datas; }
    public void setDatas(List<List<String>> datas) { this.datas = datas; }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT界的一只菜鸟

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值