以下是一个读取Excel文件并生成List的示例代码:

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.Date;
import java.util.Iterator;
import java.util.List;

public class ExcelReader {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\25346\\Desktop\\3124.xlsx";
        List<CardAttenDence> cardAttenDenceList = readExcel(filePath);
        // 打印读取结果
        for (CardAttenDence cardAttenDence : cardAttenDenceList) {
            System.out.println(cardAttenDence.getCardID() + " - " + cardAttenDence.getAttenDatatime());
        }
    }

    public static List<CardAttenDence> readExcel(String filePath) {
        List<CardAttenDence> cardAttenDenceList = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            
            // 获取第一行的列名
            Row headerRow = rowIterator.next();
            Iterator<Cell> headerCellIterator = headerRow.cellIterator();
            String cardIDColumnName = headerCellIterator.next().getStringCellValue();
            String attenDatatimeColumnName = headerCellIterator.next().getStringCellValue();

            // 遍历每一行数据
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                // 读取CardID列和AttenDatatime列的值
                String cardID = "";
                Date attenDatatime = null;
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    String columnName = headerRow.getCell(cell.getColumnIndex()).getStringCellValue();

                    if (columnName.equals(cardIDColumnName)) {
                        if (cell.getCellType() == CellType.STRING) {
                            cardID = cell.getStringCellValue();
                        } else if (cell.getCellType() == CellType.NUMERIC) {
                            cardID = String.format("%X", (int) cell.getNumericCellValue());
                        }
                    } else if (columnName.equals(attenDatatimeColumnName) && cell.getCellType() == CellType.NUMERIC) {
                        attenDatatime = cell.getDateCellValue();
                    }
                }
                // 将读取的数据添加到列表中
                if (!cardID.isEmpty() && attenDatatime != null) {
                    cardAttenDenceList.add(new CardAttenDence(cardID, attenDatatime));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cardAttenDenceList;
    }
}

class CardAttenDence {
    private String cardID;
    private Date attenDatatime;

    public CardAttenDence(String cardID, Date attenDatatime) {
        this.cardID = cardID;
        this.attenDatatime = attenDatatime;
    }

    public String getCardID() {
        return cardID;
    }

    public Date getAttenDatatime() {
        return attenDatatime;
    }
}

请注意,上述代码使用了Apache POI库来处理Excel文件。确保在项目中包含适当的POI依赖项(例如poi-ooxml)。

此代码假定Excel文件中的第一个工作表包含数据。如果您有多个工作表,请调整代码以适应不同的工作表索引或名称。

此外,代码假定CardID列为文本或数字,并将数字转换为十六进制字符串。AttenDatatime列假定为日期格式。如果列的格式与此不匹配,可能需要进行额外的处理

java 读取 3124xlsx excel文件 CUsers25346Desktop3124xlsx 表格第一行为列名第一列是 CardID 和 第二列是 AttenDatatime其中CardID列的值是16进制的数字AttenDatatime列的值是yyyy-mm-dd HHmmss的日期并避免产生Cannot get a STRING value from a NUMERIC cell

原文地址: http://www.cveoy.top/t/topic/h1Xg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录