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/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/opencsv-5.7.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

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