1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
   |  
 
package Package4;
 
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 
 
	 /**
        # *
        # * @author Pokitos
        # * Fonctionne avec la library POI. (http://poi.apache.org/hssf/index.html);
        # * Cette classe permet d'importer un fichier excel (sauf de 2007) dans un jtable.
        # */
 
 
	public class FichierXls {
 
 
	 public static  void main(String [] args ) {   
	//AvecExcel(File file) throws FileNotFoundException, IOException{ 
 
 
 
		 try {
	//Lecture du fichier excel 
	 InputStream inp = new FileInputStream("C:\\Documents and Settings\\asuissa\\Bureau\\BDD.XLS");
 
	 // recupère le fichier excel
	 HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
 
	 //Recupére page 1 du fichier xls
	 HSSFSheet sheet = wb.getSheetAt(0);
 
 
 
 
	 //compte nombre de lignes de la feuille
	 int Nrow = sheet.getLastRowNum();
	 System.out.println("LE NOMBRE DE LIGNE DE LA FEULLE EST  "+Nrow+"\n");
 
 
 
 
	 //parcours la feuille et on recupère les lignes une par une
	 for(int i = 0; i<Nrow;i++){ //for1
	 HSSFRow row = sheet.getRow(i);
 
	 //parcours la ligne pour récupérer les colonnes
 
	 if(row!=null){ //au moins une cellule dans la ligne
 
 
		 //inferieur ou egal au nombre de cellule de la ligne
	     int NombreMaxColonne=0;
	     int st=0;
 
 
 
	     //pour chaque ligne j est inf ou egal ou max de colonne
	     for(int j = 0;j<NombreMaxColonne(sheet);j++){ //for2
 
	 //Récupère la cellule puis sa valeur
 
	 HSSFCell cell = row.getCell((short)j) ;
 
	 //appelle la methode est celle ci lui affecte le bon typage
	 Object value = ContenuCellule(cell);
	 System.out.println(value.toString());
 
	 } //fin for2 
 
	 }//fin if
	 }//fin for1
 
	 inp.close();
 
	 }catch(Exception e ) {
		 e.printStackTrace();
	 }
 
 
 
	 } //fin main 
 
 
 
 
	  private static int NombreMaxColonne(HSSFSheet sheet){
 
		  int r = sheet.getLastRowNum();//recupere le dernier numero de ligne
		  int max = 0;
		  int s = 0;
		  while(s<r){
		  if(sheet.getRow(s) != null){ //recupere ligne 0
		  int c = sheet.getRow(s).getLastCellNum();
		  if(c>max){
		  max = c;
		  }
		  }
		  s++;
 
		  }
		  return max+1;
		  } 
 
	 /**
         * La cellule peut contenir différent type de valeur qui doivent être récupéré spécifiquement
        # */
public static Object ContenuCellule(HSSFCell cell ) {
	Object value=null;
 
	if(cell == null) {
		value="";
	} 
	else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
        value=cell.getBooleanCellValue();
	} 
	else if(cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
        value=cell.getErrorCellValue();
	}
	else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
        value=cell.getCellFormula();
	}
	else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
        value=cell.getNumericCellValue();
	} 
	else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
        value=cell.getRichStringCellValue();
	} 
	return value;
}
 
 
 
} | 
Partager