繁体   English   中英

从excel列提取字符串数据到hashmap时出现问题

[英]problems extracting string data from excel columns into hashmap

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

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  

我试过的

我尝试在线查找该解决方案,但是由于其特殊性,我没有找到任何解决方案。 我还尝试找到了解决方案,在这些解决方案中,键值和值都从哈希图中的excel中提取为String ,但也没有太多帮助。

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

}

聚苯乙烯

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM