2015-04-16 24 views
8

Ho un file Excel in formato .xlsx. Ho memorizzato i dati unendo le celle per formare varie colonne. Sto leggendo il file Excel tramite un'applicazione web Java e salvando i suoi dati in un database (MySQL). Ma quando leggo da celle unite ottengo valori nulli insieme a ciò che sono memorizzati nelle colonne così come le intestazioni. Sto usando Apache POI. Il mio codice è:Come leggere dalle celle unite di Excel in Java usando il POI di Apache?

public static void excelToDBLogIN() { 

    FileInputStream file = null; 
    Boolean flag = true; 
    ArrayList<String> rows = new ArrayList<String>(); 
    try { 


     // here uploadFolder contains the path to the Login 3.xlsx file 

     file = new FileInputStream(new File(uploadFolder + "Login 3.xlsx")); 

     //Create Workbook instance holding reference to .xlsx file 
     XSSFWorkbook workbook = new XSSFWorkbook(file); 

     //Get first/desired sheet from the workbook 
     XSSFSheet sheet = workbook.getSheetAt(0); 

     //Iterate through each rows one by one 
     Iterator<Row> rowIterator = sheet.iterator(); 


     while (rowIterator.hasNext()) { 
      Row row = rowIterator.next(); 

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

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

       //Check the cell type and format accordingly 
       switch (cell.getCellType()) { 

         case Cell.CELL_TYPE_NUMERIC:        

         //int value = new BigDecimal(cell.getNumericCellValue()).setScale(0, RoundingMode.HALF_UP).intValue(); 
         //tuple = tuple + String.valueOf(value) + "+"; 

         DataFormatter objDefaultFormat = new DataFormatter();  

         String str = objDefaultFormat.formatCellValue(cell); 

         tuple = tuple + str + "+"; 

         break; 

        case Cell.CELL_TYPE_STRING: 

         tuple = tuple + cell.getStringCellValue() + "+"; 

         break; 

        case Cell.CELL_TYPE_BLANK:               

         tuple = tuple + "" + "+"; 

         break; 


       } 

      } 

      rows.add(tuple); 
      flag = true; 

     } 

    }  


    } catch (Exception e) { 

     e.printStackTrace(); 

    } finally { 

     if (file != null) { 

      try { 
       file.close(); 
       file = null; 
      } catch (Exception e) { 

       System.out.println("File closing operation failed"); 
       e.printStackTrace(); 
      } 
     }         

    } 

    } 

} 

Ho cercato risposte nel web ma non ho trovato nulla di rilevante.

+2

Le celle unite sono pessime e non dovrebbero essere consentite. Evitalo. In genere, in Excel verrà archiviato il contenuto di un intervallo unito nella cella in alto a sinistra di tale intervallo. Tutte le altre celle restituiscono 0. – teylyn

+0

prova questo http://stackoverflow.com/a/27799327/624003 – Sankumarsingh

+0

Lo so, ma il formato per l'excel è stato creato dai dipartimenti del college. Il nostro progetto è semplicemente prendere informazioni da loro e aggiornare il database. Personalmente avrei evitato quelli. – Saber

risposta

6

Il seguente codice di snippet potrebbe essere d'aiuto.

while (rowIterator.hasNext()) { 
     Row row = rowIterator.next(); 

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

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

      //will iterate over the Merged cells 
      for (int i = 0; i < sheet.getNumMergedRegions(); i++) { 
       CellRangeAddress region = sheet.getMergedRegion(i); //Region of merged cells 

       int colIndex = region.getFirstColumn(); //number of columns merged 
       int rowNum = region.getFirstRow();  //number of rows merged 
       //check first cell of the region 
       if (rowNum == cell.getRowIndex() && colIndex == cell.getColumnIndex()) { 
        System.out.println(sheet.getRow(rowNum).getCell(colIndex).getStringCellValue()); 
        continue outer; 
       } 
      } 
      //the data in merge cells is always present on the first cell. All other cells(in merged region) are considered blank 
      if (cell.getCellType() == Cell.CELL_TYPE_BLANK || cell == null) { 
       continue; 
      } 
      System.out.println(cell.getStringCellValue()); 
     } 
    } 
+1

Grazie, ma ho deciso di utilizzare celle non divise in Excel. Altri, inclusi quelli qui presenti, hanno consigliato di non utilizzare i file excel di celle unite per alimentare i dati nelle applicazioni. – Saber

+2

@Deepika Cosa succede se non ci sono colonne unite nel file funzionerà il codice – Labeo

Problemi correlati