在从CSV读取时从地图存储地图中的数据

时间:2017-09-14 13:31:12

标签: java hashmap aspose

我正在开发一个项目,我需要从csv读取数据并将其存储在地图中。我使用aspose来阅读csv,请你帮我指导一下如何在Map中读取商店数据?

我尝试在地图中存储数据,然后将其再次存储在地图中,以使用列和行获取值。

链接到csv文件如下所示.. https://drive.google.com/file/d/0By64gw5ORtRjWXJ6SzZfMXd0SE0/view?usp=sharing

Map<Integer,Map<String,String>> hmap=new HashMap<Integer,Map<String,String>>();
            int startRow = 2;
            int startcol = 0;
            Integer count = 0;
            for (int i = startRow; i < trows; i++) {
                for (int j = startcol; j <= tcols-1; j++) {
                    System.out.print("----:"+ i + "    "+j);
                    map.put((String)cells.get(2, j).getValue(), (String)cells.get(i, j).getValue());
                }
                hmap.put(count, map);
                count++;
            }  

                System.out.println("map size: " + hmap.size());

输出是: -

 -----------------START------------------
    --------------------------- com.aspose.cells.Cells@5f341870
    range values in aspose: Aspose.Cells.Range [ Sheet1!A1:L14 ]
    Total Rows:14
    Total Cols:12
    map size: 12
    0 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    1 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    2 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    3 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    4 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    5 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    6 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    7 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    8 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    9 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    10 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}
    11 - {template=yes, DML=yes, prod=yes, dev=yes, stage=yes, test=yes, Dump=yes, uat=yes, Project_Name=data-portfolio-ii, Code=Yes (inside trunk), DDL=yes, local=yes}

    -----------------END------------------

感谢您提前帮助.....

1 个答案:

答案 0 :(得分:0)

您需要为每一行创建一个地图,这样就不会为最终地图中的每一行放置相同的对象:

Map<Integer,Map<String,String>> hmap=new HashMap<Integer,Map<String,String>>();
            int startRow = 2;
            int startcol = 0;
            Integer count = 0;
            for (int i = startRow; i < trows; i++) {
                Map<String,String> rowMap = new HashMap<String,String>();
                for (int j = startcol; j <= tcols-1; j++) {
                    System.out.print("----:"+ i + "    "+j);
                    rowMap .put((String)cells.get(2, j).getValue(), (String)cells.get(i, j).getValue());
                }
                hmap.put(count, rowMap );
                count++;
            }  

            System.out.println("map size: " + hmap.size());