以下是使用Apache POI库来读取Excel文件并生成CardAttenDence对象列表的示例代码:\n\njava\nimport org.apache.poi.ss.usermodel.*;\n\nimport java.io.FileInputStream;\nimport java.io.IOException;\nimport java.util.ArrayList;\nimport java.util.Date;\nimport java.util.Iterator;\nimport java.util.List;\n\npublic class ExcelReader {\n private static final String FILE_PATH = "C:\\Users\\25346\\Desktop\\3124.xlsx";\n\n public static void main(String[] args) {\n List<CardAttenDence> cardAttenDenceList = readExcelFile(FILE_PATH);\n // 打印CardAttenDence对象列表\n for (CardAttenDence cardAttenDence : cardAttenDenceList) {\n System.out.println(cardAttenDence);\n }\n }\n\n private static List<CardAttenDence> readExcelFile(String filePath) {\n List<CardAttenDence> cardAttenDenceList = new ArrayList<>();\n\n try (FileInputStream fis = new FileInputStream(filePath);\n Workbook workbook = WorkbookFactory.create(fis)) {\n\n Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet\n\n Iterator<Row> rowIterator = sheet.iterator();\n if (rowIterator.hasNext()) {\n Row headerRow = rowIterator.next();\n int cardIdColumnIndex = -1;\n int attenDatetimeColumnIndex = -1;\n\n // 查找CardID和AttenDatatime列的索引\n Iterator<Cell> cellIterator = headerRow.cellIterator();\n while (cellIterator.hasNext()) {\n Cell cell = cellIterator.next();\n String columnName = cell.getStringCellValue().trim();\n if (columnName.equalsIgnoreCase("CardID")) {\n cardIdColumnIndex = cell.getColumnIndex();\n } else if (columnName.equalsIgnoreCase("AttenDatatime")) {\n attenDatetimeColumnIndex = cell.getColumnIndex();\n }\n }\n\n // 读取数据行\n while (rowIterator.hasNext()) {\n Row row = rowIterator.next();\n\n Cell cardIdCell = row.getCell(cardIdColumnIndex);\n Cell attenDatetimeCell = row.getCell(attenDatetimeColumnIndex);\n\n String cardId = getStringValue(cardIdCell);\n String attenDatetime = getDateTimeValue(attenDatetimeCell);\n\n if (cardId != null && attenDatetime != null) {\n CardAttenDence cardAttenDence = new CardAttenDence(cardId, attenDatetime);\n cardAttenDenceList.add(cardAttenDence);\n }\n }\n }\n } catch (IOException e) {\n e.printStackTrace();\n }\n\n return cardAttenDenceList;\n }\n\n private static String getStringValue(Cell cell) {\n if (cell == null) {\n return null;\n }\n\n if (cell.getCellType() == CellType.STRING) {\n return cell.getStringCellValue();\n } else if (cell.getCellType() == CellType.NUMERIC) {\n cell.setCellType(CellType.STRING); // 将数字单元格转换为字符串单元格\n return cell.getStringCellValue();\n } else {\n return null;\n }\n }\n\n private static String getDateTimeValue(Cell cell) {\n if (cell == null) {\n return null;\n }\n\n if (cell.getCellType() == CellType.STRING) {\n return cell.getStringCellValue();\n } else if (cell.getCellType() == CellType.NUMERIC) {\n if (DateUtil.isCellDateFormatted(cell)) {\n Date date = cell.getDateCellValue();\n return date.toString();\n } else {\n return null;\n }\n } else {\n return null;\n }\n }\n}\n\n\n请确保在运行代码之前已经将Apache POI库添加到项目的依赖中。


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

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