Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1

    [Java] Leggere tutte le righe non vuote di una colonna excel

    sto usando jxl per leggere una colonna di un foglio excel.
    dovrei leggere tutte le righe di una colonna se queste non sono vuote.
    le colonne hanno solo indirizzi email, quindi sono stringhe.
    ho provato così:
    codice:
    import java.io.File;
    import java.io.IOException;
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    
    public class ExcelReader {
    
        public static void readEmail() throws IOException, BiffException {
            Workbook wb = Workbook.getWorkbook(new File("/home/matte/Desktop/email.xls"));
            Sheet sh = wb.getSheet(0);
            Cell cl = null;
            for (int i = 0; i < 100; i++) {
                if (!"".equals((sh.getCell(0, i)).getContents())) {
                    cl = sh.getCell(0, i);
                }
            }
            String address = cl.getContents();
            System.out.println(address);
            wb.close();
        }
    
        public static void main(String[] args) throws IOException, BiffException {
            readEmail();
        }
    }
    dove per ora 100 l'ho messo a mano, ma vorrei che fosse automatico.
    l'errore restituito è:
    codice:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    	at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    	at com.mattepuffo.mail.ExcelReader.readEmail(ExcelReader.java:17)
    	at com.mattepuffo.mail.ExcelReader.main(ExcelReader.java:27)
    Java Result: 1
    ma nn capisco il perchè.
    secondo voi come posso fare?

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    tanto per cominciare, metti un try catch e vedi che succede davvero...
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    codice:
    public class ExcelReader {
    
        public static void readEmail() {
            Workbook wb = null;
            try {
                wb = Workbook.getWorkbook(new File("/home/matte/Desktop/email.xls"));
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            } catch (BiffException ex) {
                System.out.println(ex.getMessage());
            }
            Sheet sh = wb.getSheet(0);
            Cell cl = null;
            for (int i = 0; i < 100; i++) {
                if (!"".equals((sh.getCell(0, i)).getContents())) {
                    cl = sh.getCell(0, i);
                }
            }
            String address = cl.getContents();
            System.out.println(address);
            wb.close();
        }
    
        public static void main(String[] args) {
            readEmail();
        }
    }
    ma l'errore nn è cambiato.

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    facciamo così, visto che l'eccezione sollevata non ha niente a che vedere, apparentemente, con quelle che cerchi di catturare tu - e quindi è annidata da qualche altra parte...
    codice:
    public static void readEmail() throws IOException, BiffException {
      try {
            Workbook wb = Workbook.getWorkbook(new File("/home/matte/Desktop/email.xls"));
            Sheet sh = wb.getSheet(0);
            Cell cl = null;
            for (int i = 0; i < 100; i++) {
                if (!"".equals((sh.getCell(0, i)).getContents())) {
                    cl = sh.getCell(0, i);
                }
            }
            String address = cl.getContents();
            System.out.println(address);
            wb.close();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
        }
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    codice:
    java.lang.ArrayIndexOutOfBoundsException: 2
    	at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    	at com.mattepuffo.mail.ExcelReader.readEmail(ExcelReader.java:19)
    	at com.mattepuffo.mail.ExcelReader.main(ExcelReader.java:32)
    diciamo che una cosa l'ho capita.
    nella colonna ci stanno solo due righe.
    se nel for metto i < 2 nn da errori e mi restituisce la seconda riga.
    da 3 in poi ho l'errore.
    forse devo prima contare le righe che nn sono vuote.
    e probabilmente nn si fa come sto provando io.

  6. #6
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    api sottomano, Sheet ha il metodo getColumn che ritorna un array di Cell:

    getColumn
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  7. #7
    ok grazie.
    ho cambiato un pò di cose.
    public Cell[] getColumn(int col) ritorna tutte le celle di una colonna.
    inoltre il metodo mi deve ritornare un ArrayList<String> che mi serve per riempire una JComboBox:

    quindi ho cambiato così:
    codice:
        public static void readEmail() throws IOException, BiffException {
            Workbook wb = Workbook.getWorkbook(new File("/home/matte/Desktop/email.xls"));
            Sheet sh = wb.getSheet(0);
            Cell[] cl = sh.getColumn(0);
            ArrayList<String> list = null;
            for (int i = 0; i < cl.length; i++) {
                list.add(cl[i].getContents());
            }
            System.out.println(list);
            wb.close();
        }
    ma ottengo sempre qusto errore:
    codice:
    Exception in thread "main" java.lang.NullPointerException
    	at com.mattepuffo.mail.ExcelReader.readEmail(ExcelReader.java:19)
    	at com.mattepuffo.mail.ExcelReader.main(ExcelReader.java:26)
    Java Result: 1
    getContents() ritorna una String: http://jexcelapi.sourceforge.net/res...Contents%28%29
    nn saprei perchè mi dice NullPointerException.
    due righe ci stanno do sicuro.

  8. #8
    Originariamente inviato da fermat
    codice:
    Exception in thread "main" java.lang.NullPointerException
    	at com.mattepuffo.mail.ExcelReader.readEmail(ExcelReader.java:19)
    	at com.mattepuffo.mail.ExcelReader.main(ExcelReader.java:26)
    Java Result: 1
    nn saprei perchè mi dice NullPointerException.
    Devi istanziare list.

  9. #9
    Originariamente inviato da VincenzoTheBest
    Devi istanziare list.
    vaf.........
    nn ci avevo proprio fatto caso.
    grazie così funziona tutto!!

  10. #10
    Originariamente inviato da fermat
    vaf.........

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.