EasyExcel浏览器上传文件读取

导入模板
EasyExcel浏览器上传文件读取_第1张图片

  1. jar包引入
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>2.2.6</version>
</dependency>
  1. 实体类
@Data//使用lombok自动生成get和set方法
public class ESolutionDetail extends BaseRowModel implements Serializable {
	/** 机构科目 */
	@ExcelProperty(index = 0)
	private String orgAccount;
	/** 机构科目备注 */
	@ExcelProperty(index = 1)
	private String orgRemark;
}
注意:2.1 实体类需要继承 com.alibaba.excel.metadata.BaseRowModel 类;
     2.2 实体类中赋值的字段要使用@ExcelProperty标注,非导入字段可以不使用该注解。
  1. 导入工具类
import com.alibaba.excel.metadata.BaseRowModel;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;

/**
 * 采用EasyExcel读取excel文件内容
 * @Author: 木易唐唐
 * @description:
 * @Date: 2021/2/24 10:41
 */
public class ReadExcel {
    public static <T> List<Object> readExcel(MultipartFile file,
                                             Class<? extends BaseRowModel> clazz) throws Exception {
        return readExcel(1,1,file,clazz);
    }

	/**
	 * sheetNo 读取excel中哪个sheet页
	 * headLineMun sheet页第几行开始读取
	*/
    public static <T> List<Object> readExcel(int sheetNo,
                                             int headLineMun,
                                             MultipartFile file,
                                             Class<? extends BaseRowModel> clazz) throws Exception {
        if (null == file) return null;
        Sheet sheet = new Sheet(sheetNo, headLineMun, clazz);
        InputStream fileInputStream = new FileInputStream(multipartFileToFile(file));
        List<Object> read = EasyExcelFactory.read(fileInputStream, sheet);
        return read;
    }

    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  1. 调用工具类
public Json subjectImport(HttpServletRequest request) throws Exception {
        Json j = new Json();
        MultipartFile file = null;
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                file = entity.getValue();// 获取上传文件对象
                List<Object> datas = ReadExcel.readExcel(file, ESolutionDetail.class);
                
        ......
  1. 小结:这是第一次使用EasyExcel,着急于完成工作内容,没有推敲细节,代码能运行起来达到预期效果后就未再处理,欢迎大家在评论区共同交流。

你可能感兴趣的:(java,excel)