Friday, May 15, 2015

Lire un fichier excel

Dans cet exemple ,on va montrer comment lire des donnés dans un fichier excel.

voici le contenu de notre fichier excel

           code   nom prenom
123   swissi ramzi
563      benfarhat mehdi
9875     test test
563    tounsi ahmed


Voici le code pour lire ce fichier
public class Parseur1 {
public static void main(String[] args) {
try {
File fichier = new File("person.xls");
// Lecture du fichier excel
InputStream inp = new FileInputStream(fichier);
// recupère le fichier excel
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
// Recupére page 1 du fichier xls
HSSFSheet sheet = wb.getSheetAt(0);
// nombre de ligne
int nbLigneFichier = sheet.getPhysicalNumberOfRows();
int nombreDeCelluleMax = 0;
for (int ligne = 0; ligne < nbLigneFichier; ligne++) {
// recuperation de chaque ligne
HSSFRow row = sheet.getRow(ligne);
// si la ligne contient au moins une cellule
if (row != null) {
if (row.getPhysicalNumberOfCells() > nombreDeCelluleMax)
nombreDeCelluleMax = row.getPhysicalNumberOfCells();
String code = row.getCell(0).toString();
System.out.println("------code---------> " + code);
String nom = "" + row.getCell(1).toString();
System.out.println("-------nom--------> " + nom);
String prenom = row.getCell(2).toString();
System.out.println("-------prenom--------> " + prenom);
}
}
inp.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
view raw person.java hosted with ❤ by GitHub

les jars utlises dans cet exemple sont :jxl.jar ,dom4j-1.6.1.jar,poi-3.5-FINAL.jar,poi-ooxml-3.5-FINAL.jar et ooxml-schemas-1.0.jar


1 comment: