栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

easyexcel 根据模板导出(excel 数据导入)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

easyexcel 根据模板导出(excel 数据导入)

阿里巴巴EasyExcel官网:链接
导入步骤:

1.导入Excel依赖
2.编写对应的Excel实体类(使用EasyExcel提供的注解)
3.编写controller
依赖
 
            com.alibaba
            easyexcel
            2.2.0-beta2
 
实体类

假设我的excel表格长这样,对应实体类写法如下

  @ExcelProperty("药物名称")  核心注解就是这个,这个注解代表着Excel中的标题,
  有几个标题,这里就得写几个注解对应在属性上
package com.cz.pojo;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;


@TableName("medices")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Medicnes {
    @TableId(type = IdType.AUTO)
    Integer id;

    @ExcelProperty("药物名称")
    String  medice;

    @ExcelProperty("药物来源")
    String  remark;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    @TableField("ctime")
    Date creatTime;
}

controller实现文件导入功能

一行代码即可把Excel中的数据转换成List集合

EasyExcel.read(file.getInputStream()).head(Medicnes.class).sheet().
doReadSync();
@RequestMapping(value = "/importExcel",method = RequestMethod.POST)
public JSonObject importExcel(@RequestParam("file") MultipartFile file)  {
        try {
            List  list = EasyExcel.read(file.getInputStream()).head(Medicnes.class).sheet().doReadSync();
            mediceService.insertBatch(list);
            return CommonResult.Sucess();
        } catch (IOException e) {
            log.info("文件导入异常{}",e);
            return  CommonResult.Failure();
        }
    }
导出功能

先上工具类ExcelUtils

package com.cz.untils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;



@Slf4j
public class ExcelUtils {
    
    public static  void Export(String fileName, Class excelHead, List data, HttpServletResponse response){
        ServletOutputStream outputStream = setResponseDownLoad(fileName, response);
        ExcelWriterBuilder writeBook = EasyExcel.write(outputStream, excelHead);
        ExcelWriterSheetBuilder sheet = writeBook.sheet(fileName);
        //文件流会自动关闭
        sheet.doWrite(data);
    }
    public static ServletOutputStream setResponseDownLoad(String fileName, HttpServletResponse response){
        try {
            //解决乱码(IE浏览器除外)
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");// 只支持字母

            //设置响应的类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置响应的编码格式
            response.setCharacterEncoding("utf8");
            //设置响应头
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            return response.getOutputStream();
        } catch (Exception e) {
            log.info("设置响应头异常{}",e);
        }
        return null;
    }
}
Controller

对应的参数说明在工具类当中,在浏览器访问即可下载文件,建议使用谷歌浏览器

 @RequestMapping(value = "/testDown",method = RequestMethod.GET)
 public  void testDown(HttpServletResponse response){
     ExcelUtils.Export("药物记录表",Medicnes.class,null,response);
 }
注意:

请使用浏览器进行下载测试,postman测试会有个小问题,名称显示乱码;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773390.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号