0
点赞
收藏
分享

微信扫一扫

java实现电子表格导入数据,不存临时文件,支持xls和xlsx

乱世小白 2024-11-02 阅读 8

引入maven依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.7</version>
</dependency>

进行文件读取,最后把读取的两个list存到map中

public Map<String, List<Map<String, String>>> readExcel(MultipartFile file) {

    String fileName = file.getOriginalFilename();

    int last = fileName.lastIndexOf(".");
    String type = fileName.substring(last + 1);

    Map<String, List<Map<String, String>>> result = new HashMap<>();
    if ("xls".equals(type)) {
        result = readXls(file);
    } else if ("xlsx".equals(type)) {
        result = readXlsx(file);
    }

    return result;
}

public Map<String, List<Map<String, String>>> readXls(MultipartFile file) {

    HSSFWorkbook workbook = null;
    try {
        workbook = new HSSFWorkbook(file.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    Map<String, List<Map<String, String>>> result = new HashMap();

    if (workbook != null) {
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();

        List<Map<String, String>> titleList = new ArrayList();
        List<Map<String, String>> dataList = new ArrayList();

        for (int a = 0; a <= lastRowNum; a++) {
            HSSFRow row = sheet.getRow(a);
            if (row != null) {
                int lastCellNum = row.getLastCellNum();

                Map<String, String> dataMap = new HashMap();

                HSSFCell firstCell = row.getCell(0);
                if (firstCell == null || (CellType.BLANK).equals(firstCell.getCellType())) {
                    continue;
                }

                for (int b = 0; b <= lastCellNum; b++) {
                    HSSFCell cell = row.getCell(b);
                    if (cell != null) {
                        if (a > 0) {
                            if (b == 0) {
                                String idno = cell.toString().replace('X', 'x');
                                dataMap.put("col" + (b + 1), idno);
                            } else {

                                String cellStr = cell.toString();
                                if (CellType.NUMERIC == cell.getCellType()) {
                                    cell.setCellType(CellType.STRING);
                                    String cellValue = cell.getStringCellValue();
                                    if (cellValue.contains(".")) {
                                        cellStr = cellValue;
                                    }
                                }

                                dataMap.put("col" + (b + 1), cellStr);
                            }
                        } else {
                            Map<String, String> titleMap = new HashMap();
                            titleMap.put("num", b + 1 + "");
                            titleMap.put("title", cell.toString());
                            titleList.add(titleMap);
                        }
                    }
                }

                if (a > 0) {
                    dataList.add(dataMap);
                }
            }
        }

        result.put("title", titleList);
        result.put("data", dataList);
    }

    return result;
}

public Map<String, List<Map<String, String>>> readXlsx(MultipartFile file) {

    XSSFWorkbook workbook = null;
    try {
        workbook = new XSSFWorkbook(file.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    Map<String, List<Map<String, String>>> result = new HashMap();

    if (workbook != null) {
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();

        List<Map<String, String>> titleList = new ArrayList();
        List<Map<String, String>> dataList = new ArrayList();

        for (int a = 0; a <= lastRowNum; a++) {
            XSSFRow row = sheet.getRow(a);
            if (row != null) {

                int lastCellNum = row.getLastCellNum();

                Map<String, String> dataMap = new HashMap();

                XSSFCell firstCell = row.getCell(0);
                if (firstCell == null || (CellType.BLANK).equals(firstCell.getCellType())) {
                    continue;
                }

                for (int b = 0; b <= lastCellNum; b++) {
                    XSSFCell cell = row.getCell(b);
                    if (cell != null) {
                        if (a > 0) {
                            if (b == 0) {
                                String idno = cell.toString().replace('X', 'x');
                                dataMap.put("col" + (b + 1), idno);
                            } else {
                                dataMap.put("col" + (b + 1), cell.toString());
                            }
                        } else {
                            Map<String, String> titleMap = new HashMap();
                            titleMap.put("num", b + 1 + "");
                            titleMap.put("title", cell.toString());
                            titleList.add(titleMap);
                        }
                    }
                }

                if (a > 0) {
                    dataList.add(dataMap);
                }
            }
        }

        result.put("title", titleList);
        result.put("data", dataList);
    }

    return result;
}

调用读取,进行解析

Map<String, List<Map<String, String>>> map = readExcel(file);

List<Map<String, String>> titleList = map.get("title");
List<Map<String, String>> dataList = map.get("data");

for (Map title : titleList) {
    SalaryPlusTitle spt = JSON.parseObject(JSON.toJSONString(title), SalaryPlusTitle.class);
    spt.setId(UUID.randomUUID().toString());
    spt.setMonth(month);
    spt.setDataobjectid(dataobjectid);
    salaryPlusTitleService.insertSalaryPlusTitle(spt);
}

for (Map data : dataList) {
    SalaryPlusData spd = JSON.parseObject(JSON.toJSONString(data), SalaryPlusData.class);
    spd.setId(UUID.randomUUID().toString());
    spd.setMonth(month);
    spd.setDataobjectid(dataobjectid);
    salaryPlusDataService.insertSalaryPlusData(spd);
}


举报

相关推荐

0 条评论