当前位置:网站首页>Use tool classes to read Excel files according to certain rules

Use tool classes to read Excel files according to certain rules

2022-06-11 04:11:00 Question mark we are friends

You need to add data in batches according to the function classification , The data carrier is Excel file . Now write a tool class to read and process according to business rules Excel File data .


Catalog

1. Tool class

2. Concrete realization

summary


1. Tool class

        Read the file here , Use hutool To process data ; adopt ExcelReader Alias handling method to give Excel Add mapping relationship to the header in , So that data can correspond to specific entity classes .

The code is as follows ( Example ):

package com.aisino.common.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.aisino.common.core.domain.entity.Inter;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by lijindong on 2022-05-30 17:36
 */
public class ExcelUtils {

    /**
     * description:  Use the tool class to get Excel Interface information in the file 
     * create by: lee
     */
    public static List<Inter> getInterrInfoByHutool(File file) {

        ExcelReader excelReader = ExcelUtil.getReader(file);
        excelReader.addHeaderAlias(" The name of the interface ","interName");
        excelReader.addHeaderAlias(" Call mode ","callMethod");
        excelReader.addHeaderAlias("URL","operUrl");
        excelReader.addHeaderAlias(" Interface status  ","interStatus");
        List<Inter> interList = excelReader.readAll(Inter.class);
        excelReader.close();
        return interList;
    }

    /**
     * MultipartFile  Convert to  File  file 
     *
     * @param multipartFile
     * @return
     */
    public final static File transferToFile(MultipartFile multipartFile) {
        // Choose to implement this transformation with a buffer, that is, use java  Temporary files created   Use  MultipartFile.transferto() Method  .
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            // Get file suffix 
            String prefix = originalFilename.substring(originalFilename.lastIndexOf("."));
            file = File.createTempFile(originalFilename, prefix);    // Create temporary file 
            multipartFile.transferTo(file);
            // Delete 
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

}

2. Concrete realization

The code is as follows ( Example ):

    List<Inter> interList;
    // from Excel Get interface information from and assemble it into the collection 
    File fileF = ExcelUtils.transferToFile(file);
    interList = ExcelUtils.getInterrInfoByHutool(fileF);

summary

That's what we're going to talk about today , This paper introduces Excel Use of tool class , It can be used to import business data in batches .

原网站

版权声明
本文为[Question mark we are friends]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110401219374.html