2017-05-04 11 views
-1

ユーザーがさまざまなページでExcelからデータをインポートして、ユーザー作成などと言うようにしたいJavaでExcelからデータを動的にインポートする方法は?

私は以下のコード(Apache POI)を試しましたが、それを動的に読み取ることはできませんでした。

ファイルの場所のURLを問い合わせています。私はそれがローカルから読んでいるならそれを指定することができます。しかし、私はこれを動的にしたい。 URLを指定する方法は?

これを処理する方法やライブラリはありますか?

public class ApachePOIExcelRead { 

//private static final String FILE_NAME = "/Users/apple/Desktop/Import.xlsx"; 

public List<User> readExcel(Import imports) { 
    System.out.println(imports.getFile().toString()); 
String FILE_NAME = "data:" + imports.getFileContentType()+";base64,"+imports.getData().toString(); 
    List<User> userList = new ArrayList<User>(); 
    try { 
     System.out.println("Import file content type is " + imports.getFileContentType()); 
     if("application/vnd.ms-excel".equalsIgnoreCase(imports.getFileContentType()) || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equalsIgnoreCase(imports.getFileContentType())){ 
     System.out.println("Content type is as exepcted"); 
     byte[] file = imports.getFile(); 
     FileInputStream excelFile = new FileInputStream(new File(FILE_NAME)); 
     excelFile.read(file); 
     Workbook workbook = new XSSFWorkbook(excelFile); 
     Sheet datatypeSheet = workbook.getSheetAt(0); 
     Iterator<Row> iterator = datatypeSheet.iterator(); 
     List<TestPOJO> pojoList = new ArrayList<TestPOJO>(); 

     while (iterator.hasNext()) { 
      System.out.println("Iterator has next row"); 
      Row currentRow = iterator.next(); 

      if(currentRow.getRowNum() != 0) { 
       //Exit the first row since its not needed. 

      Iterator<Cell> cellIterator = currentRow.iterator(); 
       TestPOJO testPOJO = new TestPOJO(); 
      while (cellIterator.hasNext()) { 

    System.out.println("cell iterator has next value"); 
       Cell currentCell = cellIterator.next(); 

       switch (currentCell.getColumnIndex()) 
       { 
        case 0 : 
         String idData = currentCell.getStringCellValue(); 
         if (DataValidator.isDataNumber(idData)) { 
          testPOJO.setId(Integer.parseInt(idData)); 
          System.out.println("Id is" +testPOJO.getId()); 

         } 
         else { 
          throw new Exception("Data not correct"); 
         } 
         break; 

        case 1: 
         String passwordData = currentCell.getStringCellValue(); 
         if(!DataValidator.isDataInvalid(passwordData)) { 
         testPOJO.setPassword(currentCell.getStringCellValue()); 
         System.out.println("password is" +testPOJO.getPassword()); 

        } 
         else { 
          throw new Exception("Data not correct"); 
         } 
         break; 
        case 2 : 
         String firstName = currentCell.getStringCellValue(); 
         if(DataValidator.isDataContainingOnlyString(firstName)) { 
          testPOJO.setFirstName(firstName); 
          System.out.println("First name is" +testPOJO.getFirstName()); 
         } 
         else { 
          throw new Exception("Data not correct"); 
         } 
         break; 
        case 3: 
         String lastName = currentCell.getStringCellValue(); 
         if(DataValidator.isDataContainingOnlyString(lastName)) { 
          testPOJO.setLastName(lastName); 
          System.out.println("Last Name is" +testPOJO.getLastName()); 
         } 
         else { 
          throw new Exception("Data not correct"); 
         } 
         break; 

        case 4 : 
         String emailId = currentCell.getStringCellValue(); 
         if(DataValidator.isDataInCorrectEmailFormat(emailId)) { 

          testPOJO.setEmailId(currentCell.getStringCellValue()); 
          System.out.println("Email Id is" +testPOJO.getEmailId()); 

         } 

         else { 
          throw new Exception("Data not correct"); 
         } 
         break; 
       } 

      } 
       pojoList.add(testPOJO); 
      } 
     } 

     System.out.println("LIST SIZE" +pojoList.size()); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return userList; 
} 
} 
+0

http://stackoverflow.com/questions/494869/file-changed-listener-in-javaを見てみましょう –

答えて

0

jarを呼び出すときに引数の1つとして渡すことはできますか?または、ファイルの場所を指定できるプロパティファイルを作成することもできます。値をハードコードする必要はありません。これがあなたの質問に答えるかどうかは分かりません。 動的データが何を意味しているかについて、より多くの情報を提供する必要があります。

+0

意味での動的データは、ユーザーが独自の値でExcelを作成することができます。それをWebページにアップロードします。値を読み取ってdbに保存しなければなりません –

+0

コードに定義されている名前で値を保存するよう依頼してください。または、ファイルをディレクトリに置くこともできます。ディレクトリにあるファイルを読み込んでアクションを実行します。または、タイムスタンプを使用して、jarを呼び出すためにcronジョブを実行することもできます。 – Mathan

+0

サーバー内のディレクトリにファイルを配置する方法。また、ユーザーがアップロードしたファイルをディレクトリに保存するにはどうすればいいですか –

関連する問題