package org.springblade.modules.project.utils;

import org.apache.poi.ss.usermodel.*;
import software.amazon.ion.util.IonStreamUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtils2 {


    public static void main(String[] args) throws Exception {

        FileInputStream da = new FileInputStream(new File("/Users/hongchuangyanfa/Desktop/excel/交通强市建设2024年工作要点.xlsx"));
        List<String> titles = new ArrayList<>();
        titles.add("序号");
        titles.add("目标任务");
        titles.add("工作内容");
        titles.add("责任单位");
        titles.add("累计进展情况");
        titles.add("存在问题");
        titles.add("工作建议");
        titles.add("填报人");
        titles.add("联系方式");
        List<Map<String, String>>  data =  readExcel(da,titles);
        System.out.println(data.size());

    }
    /**
     * 读取 Excel 文件中指定范围内的数据，并封装为 List 返回。
     *
     * @return List<Map < String, Object>> 包含读取到的数据的 List
     * @throws IOException
     */
    public static List<Map<String, String>> readExcel(InputStream is, List<String> titles) throws Exception {
        Workbook workbook = WorkbookFactory.create(is);
        Sheet sheet = workbook.getSheetAt(0);
        if (sheet == null) {
            throw new IllegalArgumentException("Excel 文件中没有 Sheet");
        }

        int firstRowIndex = sheet.getFirstRowNum();  //获取第一行的行号
        int lastRowIndex = sheet.getLastRowNum();    //获取最后一行的行号

        Row firstRow = sheet.getRow(firstRowIndex); // 获取第一行
        int firstColIndex = firstRow.getFirstCellNum();   //获取第一列的列号
        int lastColIndex = firstRow.getLastCellNum() - 1; //获取最后一列的列号

        for (int j = firstColIndex; j <= lastColIndex; j++) {
            Cell cell = firstRow.getCell(j);
            if (cell != null) {
                cell.setCellType(CellType.STRING);
                String title = cell.getStringCellValue().trim();
                titles.add(title);
            }
        }

        List<Map<String, String>> list = new ArrayList<>();
        for (int i = firstRowIndex + 1; i <= lastRowIndex; i++) {
            Row row = sheet.getRow(i);
            if (row == null) {
                continue;
            }

            Map<String, String> map = new LinkedHashMap<>();
            for (int j = firstColIndex; j <= lastColIndex; j++) {
                Cell cell = row.getCell(j);
                cell.setCellType(CellType.STRING);
                String value = getCellValue(cell);
                String key = titles.get(j - firstColIndex);
                map.put(key, value);
            }
            list.add(map);
        }

        return list;
    }


    /**
     * 获取单元格的值，并将其转换为字符串类型
     */
    private static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) {
            return String.valueOf(cell.getNumericCellValue());
        } else if (cell.getCellTypeEnum().equals(CellType.STRING)) {
            return String.valueOf(cell.getStringCellValue());
        } else if (cell.getCellTypeEnum().equals(CellType.BOOLEAN)) {
            return String.valueOf(cell.getBooleanCellValue());
        } else {
            return "";
        }
    }

}


