从excel列提取字符串数据到hashmap的问题

时间:2016-12-16 13:06:04

标签: java excel apache-poi

我正在尝试从Excel中将一些数据从excel提取到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格式,文件中没有数字类型

我想要的是什么:

我想以keyvalue对的形式将数据存储到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  

我尝试了什么:

我尝试在网上找到这个解决方案,但由于其特殊性,我找不到任何解决方案。我还尝试找到解决方案,其中键和值都从hashmap中的excel中提取为String,但也没有得到太多帮助。

我提出了一种方法,我决定将Keys存储到String,将Values存储到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();
    }

}

P.S。

  • 这是一个包含大量信号和标签的巨大文件

  • 必须使用Java完成,因为此功能将成为已构建的软件工具的一部分

  • 如果您有任何想法,您甚至可以建议我使用其他简单方法来完成此任务

3 个答案:

答案 0 :(得分:2)

// 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;
    }

答案 1 :(得分:1)

在您的代码段中,您永远不会为&#39; value&#39;创建一个空的ArrayList。对于每一行,您需要创建一个空的ArrayList,否则您无法添加值。此外,如果遍历行然后遍历单元格,则只有每行的第一个单元格包含该键。这种方式键和值始终相同。在第一个单元格条目中设置密钥后,不得覆盖密钥。

答案 2 :(得分:1)

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;
    }
}