Use ExcelUtils to get the data in the Excel file

  The pom dependencies required by ExcelUtils are as follows. This class exists under com.github.crab2died :

<!-- https://mvnrepository.com/artifact/com.github.crab2died/Excel4J -->
<dependency>
    <groupId>com.github.crab2died</groupId>
    <artifactId>Excel4J</artifactId>
    <version>3.0.0</version>
</dependency>
 List<List<String>> readExcel2List(InputStream is, int offsetLine, int limitLine, int sheetIndex)

 is : Represents the input stream of the Excel file

 offsetLine : The number of offset lines to get the data, the value starts from 0 (representing the first line), 1 represents the second line, and so on

 limitLine : limit the number of lines, which means how many lines of data after the offset line number are obtained, the value is a natural number (starting from 0)

 sheetIndex : indicates the number of sheets, the value is a positive integer

import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;
import com.github.crab2died.ExcelUtils;
import java.io.InputStream;
import java.util.*;

@RestController
@Slf4j
public class CrawlerUtilController {

    /**
     * 从Excel文件中获取数据进行业务处理
     */
    @PostMapping("/single/import1")
    public void import1(MultipartFile excelFile) {
        try{
            InputStream is = excelFile.getInputStream();
            //从文件流is中获取第一个sheet分页第2行到14320行的数据,并将每一行的数据转换为List<String>,最终将所有的行数据存储在List<List<String>>中
            List<List<String>> list= ExcelUtils.getInstance().readExcel2List(is,1,14318,1);
            for(List<String> stringList:list){
                //获取第一列的值
                String taxVouNo=stringList.get(0);
                //获取第三列的值
                String remark=stringList.get(2);
                //todo:相关的业务处理
            }
        }catch (Exception e){
            log.error("从Excel文件中获取数据进行业务处理出现异常:",e);
        }
    }
}

 The front end can use post request when calling this interface. The request body is in the form of form-data, the key is excelFile, the value is excel file, and the type is File.

Guess you like

Origin blog.csdn.net/y_bccl27/article/details/114868069