parseExcel static method

Excel parseExcel(
  1. String path
)

Charge un fichier Excel à partir de son chemin et retourne un objet Excel. Lève une exception personnalisée si le fichier est invalide.

Implementation

static Excel parseExcel(String path) {
try {
  File newFile = File(path);

  // Check if file exists
  if (!newFile.existsSync()) {
    throw ExcelParsingException("Le fichier n'existe pas");
  }

  // Check file extension
  String extension = path.split('.').last.toLowerCase();
  if (extension != "xlsx" && extension != "xls") {
    throw ExcelParsingException("Le fichier doit être au format Excel (.xlsx ou .xls)");
  }

  Excel parsedData = Excel.decodeBytes(newFile.readAsBytesSync());

  // Check if the parsed data is valid
  if (parsedData.sheets.isEmpty) {
    throw ExcelParsingException("Le fichier Excel ne contient aucune feuille");
  }

  return parsedData;
} catch (e) {
  if (e is ExcelParsingException) {
    rethrow;
  }
  throw ExcelParsingException("Erreur lors de la lecture du fichier: ${e.toString()}");
}
}