0

以下のコードから Excel ファイル データを読み取ることができますが、Excel ファイルを読み取った後に POJO クラスに格納するために Excel データを取得する方法の背後にあるロジックを取得できません。

要するに、これらの読み取ったExcelデータをモデルクラスに送信してデータベーステーブルに保存する方法がわかりませんか?

次のコードは、Eclipse コンソールに Excel の行を正しく出力します。

                     ..............
                          ..............
        public String execute()
        {
            try
            { 
                 String filePath=servletRequest.getSession().getServletContext().getRealPath("/"); 
                 File fileToCreate= new File(filePath,this.excelDataFileName);
                 FileUtils.copyFile(this.excelData, fileToCreate); 
                 UploadExcel obj=new UploadExcel();
                 obj.readExcel(excelData.getAbsolutePath()); 

            }
            catch(Exception e){
                e.printStackTrace();
                addActionError(e.getMessage());
                return INPUT;
            }
            return SUCCESS;
        }


        /*
         *Method to read the each sheet ,row & column of the excel sheet of the uploaded xls file  
        */
        public void readExcel(String filePath)
        { 
            try
             {
                FileInputStream file=new FileInputStream(new File(filePath));

                //Getting the instance for XLS file 
                 HSSFWorkbook workbook=new HSSFWorkbook(file);

                 //Get First sheet from the workbook
                HSSFSheet sheet=workbook.getSheetAt(0);

                 ArrayList myList = new ArrayList();
                //Iterate start from the first sheet of the uploaded excel file
                  Iterator<Row> rowIterator = sheet.iterator();
                  while(rowIterator.hasNext())
                  {
                      Row  row=rowIterator.next();

                      if(row.getRowNum()==0)
                      {
                          continue;//skip to read the first row of file
                      }

                      //For each row, iterate through each columns
                      Iterator<Cell> cellIterator=row.cellIterator();

                      while(cellIterator.hasNext())
                      {
                          Cell cell=cellIterator.next();
                          if(cell.getColumnIndex()==0)
                          {
                              continue;
                          }
                          switch(cell.getCellType())
                          {
                             case Cell.CELL_TYPE_BOOLEAN:
                                  System.out.print(cell.getBooleanCellValue() + "\t\t");
                                 // myList.add(cell.getBooleanCellValue());
                                  break;
                             case Cell.CELL_TYPE_NUMERIC:
                                 System.out.print(cell.getNumericCellValue()+ "\t\t");
                                //  myList.add(cell.getNumericCellValue());
                                 break;
                             case Cell.CELL_TYPE_STRING:
                                 System.out.print(cell.getStringCellValue()+ "\t\t");
                                // myList.add(cell.getStringCellValue());
                                 break;
                          }

                      }     
                      System.out.println(""); 


                  }
                  file.close();
                FileOutputStream out=
                        new FileOutputStream(new File(filePath));

                  workbook.write(out);
                  out.close();



            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }




    }

コンソール出力:

TEXTit      6695 PROSPECT RD        Nova Scotia     B3z 3t1 

row2sdfsda      61695 P         sfsdfdsf        23B3z 3t1   

私が考えたのは、行を1つずつ取得する必要があり、この行データをPOJOクラスオブジェクトに追加してdaoに送信し、最後saveOrupdate(tableobj)にhibernateメソッドを使用してデータをdbテーブルに保存することです。

しかし、これらのデータを Pojo クラスに追加する方法を考えることができませんでした。

誰かがここで私を助けてくれることを願っています。

4

2 に答える 2