从excel列中抽取string数据到hashmap的问题

我试图从Excel中提取一些数据到Java中的Hashmap。 为此,我使用的是Apache POI库,版本是Java 8.数据格式如下所示:

Excel_Data.xlsx: Title | Label A | Label B | Label C Signal 1 | value A1 | value B1 | value C1 Signal 2 | value A2 | value B2 | value C2 Signal 3 | value A3 | value B3 | value C3 

这里写的所有文本都是String格式,文件中没有数字types

我想要的是:

我想以keyvalue的forms将数据存储到Hashmap中,所以我的输出应该是:

 Expected Approach: Key -> Value Signal 1 -> [value A1, value B1, value C1 ...] Signal 2 -> [value A2, value B2, value C2 ...] Signal 3 -> [value A3, value B3, value C3 ...] 

我想实现这种方法,因为我想将这些数据按照信号顺序打印到另一个excel文件中

 Expected_output.xlsx: Signal 1 | value A1 | value B1 | value C1 Signal 2 | value A2 | value B2 | value C2 Signal 3 | value A3 | value B3 | value C3 

我试过的:

我试着在网上find这个解决scheme,但由于其特殊性,我没有find任何解决scheme。 我也试图find解决scheme,key和value都是从hashmap中的excel中提取的String ,但是也没有得到太多的帮助。

我想出了一个方法,我决定存储KeysStringValues ArrayList ,如下面的代码所示:

 // this function loads data from excel file stored in filestream variable public void storeData(){ //this hashmap will be used to hold values of excel file in key:values format HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); String key = null; ArrayList<String> value = null; try { // get workbook instance of xlsx file HSSFWorkbook workbook = new HSSFWorkbook(open_excel_data); //Get first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0); //Iterate through each rows from first sheet Iterator<Row> rowIterator = sheet.iterator(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); // for each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); key = cell.getStringCellValue(); value.add(cell.getStringCellValue()); // I cannot think of what should be here to store labels in correct format to arraylist if(key != null && value != null) { map.put(key, value); key = null; value = null; } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

PS

  • 它是一个大量的信号和标签的文件

  • 它必须使用Java来完成,因为这个function将成为已经构build的软件工具的一部分

  • 如果你有任何想法,你甚至可以build议我采取其他简单的方法来完成这项任务

 while(rowIterator.hasNext()) { Row row = rowIterator.next(); // for each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); key = null; value = new ArrayList<String>(); while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); int columnIndex = cell.getColumnIndex(); if(columnIndex == 1) { key = cell.getStringCellValue(); } else { value.add(cell.getStringCellValue()); } } if(key != null && value != null) { map.put(key, value); key = null; value = null; } } 
 // this function loads data from excel file stored in filestream variable public HashMap<String, ArrayList<String>> storeData(String fileName) { // this hashmap will be used to hold values of excel file in key:values // format HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); String key = null; ArrayList<String> value = null; int keyIndex = 0; try { // get workbook instance of xlsx file FileInputStream file = new FileInputStream(new File(fileName)); HSSFWorkbook workbook = new HSSFWorkbook(file); // Get first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0); // Iterate through each rows from first sheet Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); key = null; value = new ArrayList<String>(); // for each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); // To skip first row if (row.getRowNum() == 0){ continue; } while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); cell.setCellType(Cell.CELL_TYPE_STRING); if(cell.getColumnIndex() == keyIndex){ key = cell.getStringCellValue(); } else { value.add(cell.getStringCellValue()); } } if (key != null && value != null && value.size()>0) { map.put(key, value); key = null; value = null; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } 

在你的代码片段中,你永远不会为'value'创build一个空的ArrayList。 对于每一行你需要创build一个空的ArrayList,否则你不能添加值。 此外,如果遍历行并遍历单元格,则每行的第一个单元格中只包含密钥。 这样键和值总是一样的。 在第一个单元格条目中设置后,您不能覆盖该键。