如何通过App Script提取单元格数据为Java HashMap<String, List<String>>格式?
实现步骤与代码示例
首先得明确你的Excel单元格数据结构,下面针对两种最常见的结构给出具体实现方案,最终都能得到目标HashMap<String, List<String>>:
情况1:品牌单独一列,对应型号在同一行的多列
比如A列是品牌(如samsung),B-E列是该品牌的不同型号(Galaxy S20、Galaxy S21等),每行对应一个品牌的完整型号列表。
实现代码
使用Apache POI库读取Excel,逐行处理数据:
import org.apache.poi.ss.usermodel.*; import java.io.FileInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ExcelToHashMap { public static void main(String[] args) { Map<String, List<String>> brandMap = new HashMap<>(); try (FileInputStream fis = new FileInputStream("your-excel-file.xlsx"); Workbook workbook = WorkbookFactory.create(fis)) { Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表 for (Row row : sheet) { Cell brandCell = row.getCell(0); if (brandCell == null || brandCell.getCellType() == CellType.BLANK) { continue; // 跳过空行或无品牌的行 } String brand = brandCell.getStringCellValue().trim(); List<String> models = new ArrayList<>(); // 遍历从第二列开始的单元格,提取非空型号 for (int i = 1; i < row.getLastCellNum(); i++) { Cell modelCell = row.getCell(i); if (modelCell != null && modelCell.getCellType() == CellType.STRING && !modelCell.getStringCellValue().isEmpty()) { models.add(modelCell.getStringCellValue().trim()); } } brandMap.put(brand, models); } // 验证输出结果 brandMap.forEach((brand, models) -> { System.out.printf("{%s, %s}%n", brand, models); }); } catch (Exception e) { e.printStackTrace(); } } }
情况2:品牌列重复,每行对应一个型号
比如A列是品牌(同一品牌会重复出现),B列是对应型号,示例结构:
| samsung | Galaxy S20 |
| samsung | Galaxy S21 |
| apple | iPhone 13 |
实现代码
按品牌分组,将同一品牌的型号收集到对应列表中:
import org.apache.poi.ss.usermodel.*; import java.io.FileInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ExcelToHashMap { public static void main(String[] args) { Map<String, List<String>> brandMap = new HashMap<>(); try (FileInputStream fis = new FileInputStream("your-excel-file.xlsx"); Workbook workbook = WorkbookFactory.create(fis)) { Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { Cell brandCell = row.getCell(0); Cell modelCell = row.getCell(1); if (brandCell == null || modelCell == null || brandCell.getCellType() == CellType.BLANK || modelCell.getCellType() == CellType.BLANK) { continue; } String brand = brandCell.getStringCellValue().trim(); String model = modelCell.getStringCellValue().trim(); // 品牌不存在则新建列表,存在则追加型号 brandMap.computeIfAbsent(brand, k -> new ArrayList<>()).add(model); } // 验证输出结果 brandMap.forEach((brand, models) -> { System.out.printf("{%s, %s}%n", brand, models); }); } catch (Exception e) { e.printStackTrace(); } } }
依赖说明
如果使用Maven,需在pom.xml中添加Apache POI依赖:
<dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency> </dependencies>
额外注意事项
- 若单元格是数字格式的型号,需先转换为字符串(比如
String.valueOf(modelCell.getNumericCellValue())) - 可根据实际Excel结构调整列索引、单元格类型判断逻辑
内容的提问来源于stack exchange,提问作者user21415561
相关产品推荐
相关产品推荐

