你可以使用 Apache POI 库来读取 Excel 文件。下面是一个示例代码,可以读取指定路径下的 "3124.xlsx" 文件,并将数据存储在 List<CardAttenDence> 中:

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.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);
        }
    }

    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);

            // 获取列名行
            Row headerRow = sheet.getRow(0);

            // 获取列索引
            int cardIdColIndex = -1;
            int attenDatetimeColIndex = -1;
            Iterator<Cell> cellIterator = headerRow.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String columnName = cell.getStringCellValue();
                if (columnName.equalsIgnoreCase("CardID")) {
                    cardIdColIndex = cell.getColumnIndex();
                } else if (columnName.equalsIgnoreCase("AttenDatatime")) {
                    attenDatetimeColIndex = cell.getColumnIndex();
                }
            }

            // 读取数据行
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                if (row.getRowNum() == 0) {
                    continue; // 跳过列名行
                }

                String cardId = row.getCell(cardIdColIndex).getStringCellValue();
                String attenDatetime = row.getCell(attenDatetimeColIndex).getStringCellValue();

                // 创建 CardAttenDence 对象并添加到列表
                CardAttenDence cardAttenDence = new CardAttenDence(cardId, attenDatetime);
                cardAttenDenceList.add(cardAttenDence);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return cardAttenDenceList;
    }

    public static class CardAttenDence {
        private String cardId;
        private String attenDatetime;

        public CardAttenDence(String cardId, String attenDatetime) {
            this.cardId = cardId;
            this.attenDatetime = attenDatetime;
        }

        public String getCardId() {
            return cardId;
        }

        public String getAttenDatetime() {
            return attenDatetime;
        }

        @Override
        public String toString() {
            return "CardAttenDence{" +
                    "cardId='" + cardId + '\'' +
                    ", attenDatetime='" + attenDatetime + '\'' +
                    '}';
        }
    }
}

请确保你已将 Apache POI 库添加到项目的依赖中。这个示例代码将会读取 "3124.xlsx" 文件的第一个工作表,假设该工作表是你要读取的数据所在的工作表。如果不是,请修改 workbook.getSheetAt(0) 中的参数来指定正确的工作表索引

java 读取 3124xlsx excel文件 CUsers25346Desktop3124xlsx 表格第一行为列名第一列是 CardID 和 第二列是 AttenDatatime其中CardID列的值是16进制的数字AttenDatatime列的值是yyyy-mm-dd HHmmss的日期 生成ListCardAttenDence cardAttenDenceList = new

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

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