POI导入Excel java代码,基于POI最新版5.0.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;
    }

}


通用导出代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容