美文网首页
POI导入Excel java代码,基于POI最新版5.0.0

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

作者: 木木呦 | 来源:发表于2021-05-23 18:32 被阅读0次

    1. pom文件引入依赖

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>5.0.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>5.0.0</version>
            </dependency>
    

    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<Map<Integer, Object>> list = ExcelUtil.importExcel(multipartFile);
            System.out.println(list.size());
            for (Map<Integer, Object> 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<Map<Integer, Object>> 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<Map<Integer, Object>> list = new ArrayList<>();
    
            for (int i = 0; i < sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Integer key = 0;
                Map<Integer, Object> 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

          本文链接:https://www.haomeiwen.com/subject/wyuzjltx.html