Java.Utils:Excel转Json

Don't say much, just go to the code.

java 复制代码
package org.bood.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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

/**
 * Excel转Json
 *
 * @author bood
 * @since 2024/10/30
 */
public class ExcelToJsonConverter {

    public static void main(String[] args) {
        String excelFilePath = "/Users/bood/Downloads/*.xlsx";
        try {
            List<Map<String, Object>> jsonData = convertExcelToJson(excelFilePath);
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonData);
            System.out.println(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将Excel文件转换为JSON格式的数据
     * 此方法读取Excel文件的内容,并将其转换为一个包含多个键值对的列表
     * 每个键值对代表Excel中的一个单元格,键是列标题,值是单元格的值
     *
     * @param filePath Excel文件的路径
     * @return 包含多个键值对的列表,每个键值对代表Excel中的一个单元格
     * @author bood
     * @since 2024/12/02
     */
    public static List<Map<String, Object>> convertExcelToJson(String filePath) throws IOException {
        List<Map<String, Object>> jsonData = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream((filePath));
             Workbook workbook = new XSSFWorkbook(fis)) {
            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            Row headerRow = sheet.getRow(0);
            List<String> headers = new ArrayList<>();
            // 获取列标题(KEY)
            for (Cell cell : headerRow) {
                headers.add(cell.toString());
            }
            // 读取数据行
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;
                Map<String, Object> jsonRow = new HashMap<>();
                for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                    Cell cell = row.getCell(j);
                    jsonRow.put(headers.get(j), getCellValue(cell));
                }
                jsonData.add(jsonRow);
            }
        }
        return jsonData;
    }

    private static Object getCellValue(Cell cell) {
        switch (cell.getCellTypeEnum()) {
            case STRING:
                return cell.getStringCellValue();
            case BOOLEAN:
                return cell.getBooleanCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue();
                } else {
                    return Double.valueOf(cell.getNumericCellValue()).longValue();
                }
            case FORMULA:
                return cell.getCellFormula();
            case BLANK:
                return null;
            default:
                return null;
        }
    }

}
相关推荐
Java程序员威哥几秒前
使用Java自动加载OpenCV来调用YOLO模型检测
java·开发语言·人工智能·python·opencv·yolo·c#
小北方城市网7 分钟前
Spring Cloud Gateway实战:路由、限流、熔断与鉴权全解析
java·spring boot·后端·spring·mybatis
ZealSinger14 分钟前
Nacos2.x 事件驱动架构:原理与实战
java·spring boot·spring·spring cloud·nacos·架构·事件驱动
独行soc1 小时前
2026年渗透测试面试题总结-7(题目+回答)
java·网络·python·安全·web安全·渗透测试·安全狮
007php0072 小时前
PHP与Java项目在服务器上的对接准备与过程
java·服务器·开发语言·分布式·面试·职场和发展·php
sheji34162 小时前
【开题答辩全过程】以 民宿预订管理系统的设计与实现为例,包含答辩的问题和答案
java
刘大猫.3 小时前
XNMS项目-拓扑图展示
java·人工智能·算法·拓扑·拓扑图·节点树·xnms
正在努力Coding9 小时前
SpringAI - 工具调用
java·spring·ai
我尽力学9 小时前
面试 总结
java·spring boot·面试
爬台阶的蚂蚁9 小时前
Spring AI Alibaba基础概念
java·spring·ai