Browse Source

excel mety

31052025_01
b.razafimandimbihery 6 months ago
parent
commit
e1de473768
  1. 136
      lib/Views/HandleProduct.dart

136
lib/Views/HandleProduct.dart

@ -102,84 +102,84 @@ void _showExcelCompatibilityError() {
} }
Future<void> _downloadExcelTemplate() async { Future<void> _downloadExcelTemplate() async {
try { try {
// Créez un nouveau fichier Excel // Créer un fichier Excel temporaire comme modèle
final excel = Excel.createExcel(); final excel = Excel.createExcel();
// Supprimez la feuille par défaut si elle existe // // Renommer la feuille par défaut
excel.delete('Sheet1'); // excel.rename('Sheet1', 'Produits');
// Créez une nouvelle feuille nommée "Produits" // Accéder à la feuille renommée
final sheet = excel['Produits']; final sheet = excel['Sheet1'];
// Ajoutez les en-têtes // Ajouter les en-têtes avec du style
final headers = ['Nom', 'Prix', 'Catégorie', 'Description', 'Stock']; final headers = ['Nom', 'Prix', 'Catégorie', 'Description', 'Stock'];
for (int i = 0; i < headers.length; i++) { for (int i = 0; i < headers.length; i++) {
final cell = sheet.cell(CellIndex.indexByColumnRow(columnIndex: i, rowIndex: 0)); final cell = sheet.cell(CellIndex.indexByColumnRow(columnIndex: i, rowIndex: 0));
cell.value = headers[i]; cell.value = headers[i];
cell.cellStyle = CellStyle( cell.cellStyle = CellStyle(
bold: true, bold: true,
backgroundColorHex: '#E8F4FD', backgroundColorHex: '#E8F4FD',
); );
} }
// Ajoutez des exemples de données // Ajouter des exemples
final examples = [ final examples = [
['Croissant', '1.50', 'Sucré', 'Délicieux croissant beurré', '20'], ['Croissant', '1.50', 'Sucré', 'Délicieux croissant beurré', '20'],
['Sandwich jambon', '4.00', 'Salé', 'Sandwich fait maison', '15'], ['Sandwich jambon', '4.00', 'Salé', 'Sandwich fait maison', '15'],
['Jus d\'orange', '2.50', 'Jus', 'Jus d\'orange frais', '30'], ['Jus d\'orange', '2.50', 'Jus', 'Jus d\'orange frais', '30'],
['Gâteau chocolat', '18.00', 'Gateaux', 'Gâteau au chocolat portion 8 personnes', '5'], ['Gâteau chocolat', '18.00', 'Gateaux', 'Gâteau au chocolat portion 8 personnes', '5'],
]; ];
for (int row = 0; row < examples.length; row++) { for (int row = 0; row < examples.length; row++) {
for (int col = 0; col < examples[row].length; col++) { for (int col = 0; col < examples[row].length; col++) {
final cell = sheet.cell(CellIndex.indexByColumnRow(columnIndex: col, rowIndex: row + 1)); final cell = sheet.cell(CellIndex.indexByColumnRow(columnIndex: col, rowIndex: row + 1));
cell.value = examples[row][col]; cell.value = examples[row][col];
}
} }
}
// Définissez la largeur des colonnes // Ajuster la largeur des colonnes
sheet.setColWidth(0, 20); sheet.setColWidth(0, 20); // Nom
sheet.setColWidth(1, 10); sheet.setColWidth(1, 10); // Prix
sheet.setColWidth(2, 15); sheet.setColWidth(2, 15); // Catégorie
sheet.setColWidth(3, 30); sheet.setColWidth(3, 30); // Description
sheet.setColWidth(4, 10); sheet.setColWidth(4, 10); // Stock
// Sauvegardez le fichier Excel // Sauvegarder en mémoire
final bytes = excel.save(); final bytes = excel.save();
if (bytes == null) { if (bytes == null) {
Get.snackbar('Erreur', 'Impossible de créer le fichier modèle'); Get.snackbar('Erreur', 'Impossible de créer le fichier modèle');
return; return;
} }
// Demandez à l'utilisateur où sauvegarder le fichier // Demander sauvegarder
final String? outputFile = await FilePicker.platform.saveFile( final String? outputFile = await FilePicker.platform.saveFile(
fileName: 'modele_import_produits.xlsx', fileName: 'modele_import_produits.xlsx',
allowedExtensions: ['xlsx'], allowedExtensions: ['xlsx'],
type: FileType.custom, type: FileType.custom,
); );
if (outputFile != null) { if (outputFile != null) {
try { try {
// Écrivez les données dans le fichier await File(outputFile).writeAsBytes(bytes);
await File(outputFile).writeAsBytes(bytes); Get.snackbar(
Get.snackbar( 'Succès',
'Succès', 'Modèle téléchargé avec succès\n$outputFile',
'Modèle téléchargé avec succès\n$outputFile', duration: const Duration(seconds: 4),
duration: const Duration(seconds: 4), backgroundColor: Colors.green,
backgroundColor: Colors.green, colorText: Colors.white,
colorText: Colors.white, );
); } catch (e) {
} catch (e) { Get.snackbar('Erreur', 'Impossible d\'écrire le fichier: $e');
Get.snackbar('Erreur', 'Impossible d\'écrire le fichier: $e'); }
} }
} catch (e) {
Get.snackbar('Erreur', 'Erreur lors de la création du modèle: $e');
debugPrint('Erreur création modèle Excel: $e');
} }
} catch (e) {
Get.snackbar('Erreur', 'Erreur lors de la création du modèle: $e');
debugPrint('Erreur création modèle Excel: $e');
} }
}
Future<void> _importFromExcel() async { Future<void> _importFromExcel() async {

Loading…
Cancel
Save