Java - Excel解析示例

Java - Excel解析示例

package com.****.****.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author wintersakura
 * @version 1.0
 * @date 2023/7/16 10:47
 */

@Api(tags = "*****-EXCEL")
@RestController
@RequestMapping("/xxxx")
public class ExcelImportController {

    /**
     * 导入excel
     *
     * @param file
     */
    @PostMapping("/import")
    public void importExcel(MultipartFile file) throws IOException {
        //1.第一种 头必须和实体(英文)一样
        //文件处理成io流
        InputStream in = file.getInputStream();
//        //io流给ExcelReader
        ExcelReader excelReader = ExcelUtil.getReader(in, 0);

        //2.第二种导入方式
        //忽略第一行头(第一行是中文的情况),直接读取表的内容
        List<List<Object>> list = excelReader.read(1);
        for (int i = 1; i < list.size(); i++) {
            List<Object> row = list.get(i);
            //System.out.print("第" + (i + 1) + "行,");

            for (int j = 0; j < row.size(); j++) {
                String spanValue = row.get(j) + "";
                //System.out.print(spanValue + ", ");
                System.out.print("第" + (i + 1) + "行,第" + (j + 1) + "列,值:" + spanValue);

            }

        }

    }

}