POI导入Excel java代码,基于POI最新版5.0.0

1. pom文件引入依赖

        
            org.apache.poi
            poi
            5.0.0
        

        
            org.apache.poi
            poi-ooxml
            5.0.0
        

2. 拷贝代码

package com.mutu.admin.controller.common;

import com.mutu.admin.utils.ExcelUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("file/v1/*")
public class FileController {

    @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {

        System.out.println("文件导入名称:" + multipartFile.getOriginalFilename());
        List> list = ExcelUtil.importExcel(multipartFile);
        System.out.println(list.size());
        for (Map integerObjectMap : list) {
            System.out.println(integerObjectMap.toString());
        }

    }

}

package com.mutu.admin.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtil {

    public static List> importExcel(MultipartFile multipartFile) throws IOException {

        String filePath = multipartFile.getOriginalFilename();
        InputStream ips = multipartFile.getInputStream();
        Workbook wb = null;
        if (filePath.endsWith(".xlsx")){
            wb = new XSSFWorkbook(ips);
        }else if (filePath.endsWith(".xls")){
            wb = new HSSFWorkbook(ips);
        }else {
            throw new NoSuchMethodError("没有能够处理此文件的方法");
        }

        Sheet sheet = wb.getSheetAt(0);

        List> list = new ArrayList<>();

        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            Integer key = 0;
            Map map = new HashMap<>();
            for (Cell cell : row) {
                switch (cell.getCellType()) {
                    case BOOLEAN:
                        map.put(key, cell.getBooleanCellValue());
                        break;
                    case NUMERIC:
                        //先看是否是日期格式
                        if (DateUtil.isCellDateFormatted(cell)) {
                            //读取日期格式
                            map.put(key, cell.getDateCellValue());

                        } else {
                            //读取数字
                            map.put(key, cell.getNumericCellValue());

                        }
                        break;
                    case FORMULA:
                        //读取公式
                        map.put(key, cell.getCellFormula());
                        break;
                    case STRING:
                        //读取String
                        map.put(key, cell.getStringCellValue());
                        break;
                    case ERROR:
                    case _NONE:
                    case BLANK:
                        map.put(key, null);
                        break;
                    default:
                        map.put(key, null);
                }
                key++;
            }
            list.add(map);
        }
        return list;
    }

}


通用导出代码

你可能感兴趣的:(POI导入Excel java代码,基于POI最新版5.0.0)