【发布时间】:2020-05-27 13:47:12
【问题描述】:
我正在使用 JAVA 创建自动化框架,并将测试数据源用作 excel 表。目前将所有数据从 excel 表中提取到 LinkedHashmap 并在代码中使用 linkedhashmap。我的问题是了解从 excelsheet 中提取数据的有效方法。 我的 HashMap 看起来像
public static HashMap<Integer, ArrayList<String>> exceldata = new LinkedHashMap<Integer, ArrayList<String>>();
该行的行号和数据。 我正在使用 apache POI 从 excel 表中获取数据。但是我必须将其存储在数据结构中才能执行一些元数据。
File fs=new File(AppVariableConstants.TestDataPath);
FileInputStream fis = new FileInputStream(fs);
HSSFWorkbook workbook = new HSSFWorkbook (fis);
HSSFSheet sheet = workbook.getSheetAt(0);
int rows; // No of rows
int cols;
String KeyHeader;
String ValueCell=null;
rows = sheet.getPhysicalNumberOfRows();
cols=sheet.getRow(0).getPhysicalNumberOfCells();
public static HashMap<Integer, ArrayList<String>> exceldata = new LinkedHashMap<Integer, ArrayList<String>>();
for(int i=1; i<rows; i++){
ArrayList<String> list1 = new ArrayList<String>();
for(int j=0; j<cols ;j++) {
KeyHeader=sheet.getRow(0).getCell(j).getStringCellValue();
switch(sheet.getRow(i).getCell(j).getCellType()) {
case NUMERIC:
ValueCell=String.valueOf(sheet.getRow(i).getCell(j).getNumericCellValue());
break;
exceldata.put(i, list1);// row number and all data for that row
【问题讨论】:
-
尝试使用 Apache POI 等开源库之一。不要重新发明轮子。
-
我正在使用 apache POi 读取数据,我只想将其保存在数据结构中以供进一步使用。
-
HashMap 看起来不错,但实际上取决于您将如何使用数据。