version avec extraction de csv buggé par les virgules

This commit is contained in:
Qpelu 2025-03-04 11:56:19 +01:00
parent 3da62feb2a
commit 0e0e415d48
4 changed files with 44 additions and 60 deletions

View File

@ -28,5 +28,6 @@
<classpathentry kind="lib" path="C:/Users/Qpelu/Documents/Eclypse/poi-bin-5.2.3-20220909/poi-bin-5.2.3/ooxml-lib/slf4j-api-1.7.36.jar"/> <classpathentry kind="lib" path="C:/Users/Qpelu/Documents/Eclypse/poi-bin-5.2.3-20220909/poi-bin-5.2.3/ooxml-lib/slf4j-api-1.7.36.jar"/>
<classpathentry kind="lib" path="C:/Users/Qpelu/Documents/Eclypse/poi-bin-5.2.3-20220909/poi-bin-5.2.3/ooxml-lib/xmlbeans-5.1.1.jar"/> <classpathentry kind="lib" path="C:/Users/Qpelu/Documents/Eclypse/poi-bin-5.2.3-20220909/poi-bin-5.2.3/ooxml-lib/xmlbeans-5.1.1.jar"/>
<classpathentry kind="lib" path="C:/Users/Qpelu/Downloads/log4j-core-2.9.1.jar"/> <classpathentry kind="lib" path="C:/Users/Qpelu/Downloads/log4j-core-2.9.1.jar"/>
<classpathentry kind="lib" path="C:/Users/Qpelu/Downloads/opencsv-5.7.1.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -3,13 +3,15 @@ package Main;
public class Avis { public class Avis {
private String commentaire; private String commentaire;
private int positif; private int positif;
private int film; private int film;
private String url;
public Avis(String com, int pos, int f) { public Avis(int f,String u, String com, int pos ) {
commentaire = com; commentaire = com;
positif = pos; positif = pos;
film = f; film = f;
url =u;
} }
@ -47,6 +49,16 @@ public class Avis {
public String toString() { public String toString() {
return "Avis{commentaire='" + commentaire + "', positif=" + positif + ", film=" + film + "}"; return "Avis{commentaire='" + commentaire + "', positif=" + positif + ", film=" + film + "}";
} }
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
} }

View File

@ -1,85 +1,56 @@
package Main; package Main;
import org.apache.poi.ss.usermodel.*; import java.io.BufferedReader;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ListeAvis { public class ListeAvis {
private List<Avis> listeAvis; private List<Avis> listeAvis;
private int colCommentaire;
private int colPositivite;
private int colFilm;
// Constructeur qui charge le fichier Excel public ListeAvis(String csvPath) {
public ListeAvis(String aPath) {
listeAvis = new ArrayList<>(); listeAvis = new ArrayList<>();
colCommentaire = 2; lireCSV(csvPath);
colPositivite = 3;
colFilm = 4;
lireExcel(aPath);
} }
// Fonction qui lit le fichier Excel et charge les avis private void lireCSV(String filePath) {
private void lireExcel(String filePath) { try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
try (FileInputStream fis = new FileInputStream(new File(filePath)); String ligne;
Workbook workbook = new XSSFWorkbook(fis)) { br.readLine();
Sheet sheet = workbook.getSheetAt(0); // On prend la première feuille while ((ligne = br.readLine()) != null) {
System.out.println("Fichier Excel chargé avec succès!"); String[] valeurs = ligne.split(",");
if (valeurs.length < 4) continue;
for (Row row : sheet) { try {
if (row.getRowNum() == 0) continue; // Ignorer la première ligne (en-têtes) int numeroFilm = Integer.parseInt(valeurs[0].trim());
String url = valeurs[1].trim();
String commentaire = valeurs[2].trim();
int polarite = Integer.parseInt(valeurs[valeurs.length-1].trim());
// Extraction des données listeAvis.add(new Avis(numeroFilm, url, commentaire, polarite));
String commentaire = getCellValue(row.getCell(colCommentaire)); } catch (NumberFormatException e) {
int positivite = getIntCellValue(row.getCell(colPositivite)); System.err.println("Erreur de format dans la ligne : " + ligne);
int film = getIntCellValue(row.getCell(colFilm)); }
// Ajout de l'avis à la liste
listeAvis.add(new Avis(commentaire, positivite, film));
// Affichage pour débogage
System.out.println("Commentaire: " + commentaire);
System.out.println("Positivité: " + positivite);
System.out.println("Film: " + film);
} }
System.out.println("Fichier CSV chargé avec succès !");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
// Fonction pour récupérer la valeur d'une cellule sous forme de String public void afficherAvis() {
private String getCellValue(Cell cell) { for (Avis avis : listeAvis) {
if (cell == null) return ""; System.out.println("Numéro du film: " + avis.getFilm());
if (cell.getCellType() == CellType.STRING) { System.out.println("URL du film: " + avis.getUrl());
return cell.getStringCellValue(); System.out.println("Commentaire: " + avis.getCommentaire());
} else if (cell.getCellType() == CellType.NUMERIC) { System.out.println("Positivité: " + avis.getPositif());
return String.valueOf((int) cell.getNumericCellValue()); System.out.println("------------------------");
} }
return "";
} }
// Fonction pour récupérer la valeur d'une cellule sous forme d'entier
private int getIntCellValue(Cell cell) {
if (cell == null || cell.getCellType() != CellType.NUMERIC) return 0;
return (int) cell.getNumericCellValue();
}
public List<Avis> getListeAvis() { public List<Avis> getListeAvis() {
return listeAvis; return listeAvis;
} }
public void afficherAvis() {
for (Avis avis : listeAvis) {
System.out.println("Commentaire: " + avis.getCommentaire());
System.out.println("Positivité: " + avis.getPositif());
System.out.println("Film: " + avis.getFilm());
}
}
} }

View File

@ -3,7 +3,7 @@ package Main;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
String cheminFichierExcel = "C:\\Users\\Qpelu\\Downloads\\test_avis.xlsx"; String cheminFichierExcel = "C:\\Users\\Qpelu\\Downloads\\mini.csv";
ListeAvis listeAvis = new ListeAvis(cheminFichierExcel); ListeAvis listeAvis = new ListeAvis(cheminFichierExcel);