开发者你好:使用excel导入中有一个问题:数据解析时会将excel中行首的下拉选择框全部解析一遍,如何忽略下拉选择框的解析,只解析正文呢?(例:有一列民族框,下拉选择有56个,结果解析出来就会多出56个空对象)
lawou6xi1#
import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ExcelParser { public static List<Object[]> parseExcel(String filename) throws IOException { List<Object[]> parsedData = new ArrayList<>(); // 创建文件输入流 FileInputStream fis = new FileInputStream(new File(filename)); Workbook workbook = WorkbookFactory.create(fis); // 获取第一个工作表 Sheet sheet = workbook.getSheetAt(0); // 迭代行 Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); // 检查每行的第一个单元格是否包含有效数据 Cell firstCell = row.getCell(0); if (firstCell != null && firstCell.getCellType() != CellType.BLANK) { List<Object> rowData = new ArrayList<>(); // 迭代列 Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); // 将单元格值添加到行数据中 switch (cell.getCellType()) { case STRING: rowData.add(cell.getStringCellValue()); break; case NUMERIC: rowData.add(cell.getNumericCellValue()); break; // 根据需要添加更多单元格类型的处理逻辑 default: rowData.add(""); // 或者添加其他默认值 break; } } parsedData.add(rowData.toArray()); } } workbook.close(); fis.close(); return parsedData; } public static void main(String[] args) { try { String filename = "your_excel_file.xlsx"; List<Object[]> parsedData = parseExcel(filename); for (Object[] row : parsedData) { for (Object cellValue : row) { System.out.print(cellValue + "\t"); } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } } }
1条答案
按热度按时间lawou6xi1#