|
|
|
|
Configuration: Windows XP Internet Explorer 6.0
J'utilise deux librairies gratuites trouvées sur le web :
- Une pour lire le fichier Excel : http://poi.apache.org/hssf/index.html - L'autre pour écrire le fichier CSV (pas nécessairement utile) : http://opencsv.sourceforge.net/ Voici le code : import au.com.bytecode.opencsv.CSVWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Vector; 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; public class TestXLS { private POIFSFileSystem fs = null; private HSSFWorkbook hssfworkbook = null; private Vector lignes = null; public TestXLS(String file) { try{ fs = new POIFSFileSystem(new FileInputStream(file)); hssfworkbook = new HSSFWorkbook(fs); lignes = new Vector(); } catch(Exception e) { System.err.println("Exception constructeur : "+e.getMessage()); System.exit(-1); } } public void lecture() { for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) { HSSFSheet sheet = hssfworkbook.getSheetAt(i); int rows = sheet.getPhysicalNumberOfRows(); for(int j=0;j<rows;j++) { Vector temp = new Vector(); HSSFRow row = sheet.getRow(j); int cells = row.getPhysicalNumberOfCells(); for(short k=0;k<cells;k++) { HSSFCell cell = row.getCell(k); String value = null; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA : value = cell.getCellFormula(); temp.add(value); break; case HSSFCell.CELL_TYPE_NUMERIC : value = ""+cell.getNumericCellValue(); temp.add(value); break; case HSSFCell.CELL_TYPE_STRING : value = cell.getRichStringCellValue().getString(); temp.add(value); break; default : value =""; } } lignes.add(temp); } } } public void ecrireCSV(String file) { CSVWriter writer = null; try{ writer = new CSVWriter(new FileWriter(file), ';'); for(int i=0;i<lignes.size();i++) { Vector temp = (Vector)lignes.get(i); Object[] entree = (Object[])temp.toArray(); ecrireString(writer, entree); } writer.close(); } catch(IOException ex) { System.err.println("Erreur lors de l'écriture : "+ex.getMessage()); } } private void ecrireString(CSVWriter writer, Object[] entree){ String[] tab = new String[entree.length]; for(int i=0;i<entree.length;i++) tab[i] = (String)entree[i]; writer.writeNext(tab); } public Vector getLignes(){return lignes;} public static void main(String[] args) { TestXLS test = new TestXLS("D:\\test.xls"); test.lecture(); test.ecrireCSV("D:\\test.txt"); } } -- Alex pour vous servir -- |
salut ca va moi boubakr de agadir msn love_morad58@hotmail.com oki bey bizooooooooooo |
Résultats pour JAVA Convertir un file xls to csv
Résultats pour JAVA Convertir un file xls to csv
Résultats pour JAVA Convertir un file xls to csv
Résultats pour JAVA Convertir un file xls to csv
Résultats pour JAVA Convertir un file xls to csv
Résultats pour JAVA Convertir un file xls to csv
Résultats pour JAVA Convertir un file xls to csv