dimanche 22 juillet 2018

What is the best design pattern to read an excel with multiple sheets

I have an excel sheet with a standard format that will have multiple sheets. I need to read the sheets in Java and convert them to Objects and store in the database. I and currently using the Simple Factory pattern for this. I am reading the sheet name and invoking the corresponding converter objects based on the sheet name. Is this the only way to achieve this or is there a better way. Below is the sample code.

FileInputStream fis = new FileInputStream(new File("test.xls"));
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fis);
        int numberOfSheets = hssfWorkbook.getNumberOfSheets();
        for (int i=0; i<numberOfSheets; i++) {
           HSSFSheet sh =  hssfWorkbook.getSheetAt(i);
           String sname = sh.getSheetName();
           switch (sname) {
               case "Test" : //go to test convertor;
               case "Test1": //go to test1 convertor
           }
        }

Aucun commentaire:

Enregistrer un commentaire