【问题标题】:Storing data to Java Collection from Excel file将数据从 Excel 文件存储到 Java 集合
【发布时间】:2016-05-05 14:30:46
【问题描述】:

我想从 Excel 文件中读取数据,并希望将诸如 HashMap 之类的东西存储到 Java 集合中。

我已经通过 Apache POI 实现了 Excel 读取——这部分工作得很好。 现在我想知道如何将这些数据存储到 HashMap 中。

我创建了 1 个具有 Excel 数据值的 POJO 类。

Class EmpXcel
    private String emp_name;
    private String emp_job;

     // parameterised constructor

     // getter-setter

现在如果我创建这个特定 POJO 类的类型的 HashMap。

例如。 Map<String, EmpXcel> map = new HashMap<String, EmpXcel>();

这是我从 Excel 读取数据的代码。

while(itr.hasNext()){
            Row nextRow = itr.next();
            // For each row, iterate through all the columns
            Iterator<Cell> cellIterator = nextRow.cellIterator();

            while (cellIterator.hasNext()) 
            {
                Cell cell = cellIterator.next();

                switch (cell.getCellType()) 
                {
                    //  if cell is numeric format   
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue()+",");
                        break;

                    // if cell is string format
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + ",");
                        break;
                }
            } 
         System.out.println();
    }

我不确定如何将此数据存储到 POJO 类(此处为 EmpXcel)类型的 HashMap 中。

编辑:Excel 表格:

Emp_Name |  Emp_Job

Travis   |  Technical Assistant

John     |  Professor

我还想让它更通用-想为不同的 Excel 文件创建单个 Java 类(不考虑行数/列数)

请与我分享你的一些见解,以便我可以按照这种方式进行操作。

谢谢。

【问题讨论】:

  • excel中的数据是什么类型的?
  • 为什么要使用HashMap?顺序不可预测。
  • @java8.being。字符串类型...用 Excel 表格示例更新问题
  • 查看数据似乎根本不需要地图。这也可以提取到列表中
  • 目前没有大约 10k 条记录。如果我按照您的建议实现 ArrayList,我将如何将每列的记录添加到其中。例如。 _我从 Excel Emp_Name 和 Emp_Job 读取-我应该如何添加并在稍后的时间点检索?_>

标签: java excel collections hashmap apache-poi


【解决方案1】:

回答您的上一条评论@inityk 通过使用 getColumnIndex(),您可以访问所有单元格,然后可以验证其中存在的数据。

List<EmpXcel> empXcelList=new Arrayist<EmpXcel>();
   while(itr.hasNext()){
        Row nextRow = itr.next();
        EmpXcel empXcelPOJO = new EmpXcel();
        // For each row, iterate through all the columns
        Iterator<Cell> cellIterator = nextRow.cellIterator();

        while (cellIterator.hasNext()) 
        {
            Cell cell = cellIterator.next();

            if(cell.getColumnIndex()==0) 
            {
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print("Name should not be numeric");
                    break;

                // if cell is string format
                case Cell.CELL_TYPE_STRING:
                    empXcelPOJO.setEmpName(cell.getStringCellValue())
                    break;
            }elseif(cell.getColumnIndex()==1) 
            {
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print("Name should not be numeric");
                    break;

                // if cell is string format
                case Cell.CELL_TYPE_STRING:
                    empXcelPOJO.setEmpJob(cell.getStringCellValue())
                    break;
            }
        } 
     empXcelList.add(empXcelPOJO);
    }

【讨论】:

  • cell.getColumnIndex()==0 和 nextRow.getCell(0) 一样吗?似乎他们俩都返回或检查第一个单元格。如果您认为我错了,请纠正我
  • +1 指出验证!此外,我想做的是为 Mutilple excel 文件创建单个 Java 类(无论计数如何-我应该如何实现)
  • 请详细解释上述查询。就像您所说的用于多个文件的 java 类是什么意思?这是否意味着一个 pojo 类来存储包含不同数据的多个 excel 的数据或用于处理不同 excel 的单个处理器正在从中创建 java 对象的集合?请告诉我你说的是哪一门课。
  • 此外,如果两个excel一起包含不同的信息,则使用不同的类(POJO)来存储信息。要使用单个类存储信息,excel中的列顺序应该相同。
  • 是的,我也有同样的感觉——但是我们在 POI 中是否有任何规定可以获取整行并将行的所有这些值存储到 ArrayList 或其他集合中?
猜你喜欢
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
相关资源
最近更新 更多