ajout de l'import en XLSX
parent
7aa1bb2759
commit
45a9304c41
|
@ -37,6 +37,8 @@ dependencies {
|
|||
implementation("io.ktor:ktor-html-builder:1.6.3")
|
||||
implementation("junit:junit:4.13.1")
|
||||
testImplementation("junit:junit:4.13.1")
|
||||
implementation("org.apache.poi:poi:5.0.0")
|
||||
implementation("org.apache.poi:poi-ooxml:5.0.0")
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,10 @@ import java.awt.Frame
|
|||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.IOException
|
||||
import org.apache.poi.ss.usermodel.CellType
|
||||
import org.apache.poi.ss.usermodel.Workbook
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook
|
||||
import java.io.FileInputStream
|
||||
|
||||
val customRedColor = Color(0xFFB70D1B)
|
||||
|
||||
|
@ -95,9 +99,9 @@ fun app() {
|
|||
glossairePage(
|
||||
onAjouterMotClick = { currentPage.value = "formulaire" },
|
||||
onImporterClick = {
|
||||
selectFile(setOf("csv")) { filePath ->
|
||||
selectFile(setOf("csv", "xlsx")) { filePath ->
|
||||
println("Importing file: $filePath")
|
||||
importCSV(filePath)
|
||||
importFile(filePath)
|
||||
}
|
||||
},
|
||||
onExporterClick = {
|
||||
|
@ -196,7 +200,23 @@ fun exportToCSV(csvFilePath: String) {
|
|||
}
|
||||
}
|
||||
|
||||
fun importCSV(cheminFichier: String) {
|
||||
fun importFile(cheminFichier: String) {
|
||||
val fileExtension = File(cheminFichier).extension.lowercase()
|
||||
|
||||
when {
|
||||
fileExtension == "csv" -> {
|
||||
importCSVFile(cheminFichier)
|
||||
}
|
||||
fileExtension == "xlsx" -> {
|
||||
importXLSXFile(cheminFichier)
|
||||
}
|
||||
else -> {
|
||||
println("Unsupported file format.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun importCSVFile(cheminFichier: String) {
|
||||
val file = File(cheminFichier).readText(Charsets.UTF_8)
|
||||
|
||||
val lines = file.split("\n")
|
||||
|
@ -236,6 +256,63 @@ fun importCSV(cheminFichier: String) {
|
|||
}
|
||||
}
|
||||
|
||||
private fun importXLSXFile(cheminFichier: String) {
|
||||
val workbook: Workbook
|
||||
try {
|
||||
FileInputStream(cheminFichier).use { fileInputStream ->
|
||||
workbook = XSSFWorkbook(fileInputStream)
|
||||
}
|
||||
|
||||
val sheet = workbook.getSheetAt(0) // Assuming the data is in the first sheet
|
||||
|
||||
val headerRow = sheet.getRow(0)
|
||||
val header = mutableListOf<String>()
|
||||
for (i in 0 until headerRow.lastCellNum) {
|
||||
header.add(headerRow.getCell(i).stringCellValue)
|
||||
}
|
||||
|
||||
val dataLines = mutableListOf<Map<String, String>>()
|
||||
|
||||
for (i in 1 until sheet.physicalNumberOfRows) {
|
||||
val currentRow = sheet.getRow(i)
|
||||
val data = mutableMapOf<String, String>()
|
||||
|
||||
for (j in 0 until currentRow.lastCellNum) {
|
||||
val cell = currentRow.getCell(j)
|
||||
val cellValue = when (cell.cellType) {
|
||||
CellType.STRING -> cell.stringCellValue
|
||||
CellType.NUMERIC -> cell.numericCellValue.toString()
|
||||
else -> ""
|
||||
}
|
||||
data[header[j]] = cellValue
|
||||
}
|
||||
|
||||
dataLines.add(data)
|
||||
}
|
||||
|
||||
dataLines.forEach { line ->
|
||||
// Check if the line is not empty or blank
|
||||
if (line.isNotEmpty()) {
|
||||
// Process each line as needed
|
||||
val nouveauMot = Mot(
|
||||
nom = line["Mot"] ?: "",
|
||||
description = line["Description"] ?: "",
|
||||
contextePrincipal = line["Contexte principal"] ?: "",
|
||||
contexte2 = line["Contexte 2"] ?: "",
|
||||
lieA = line["Lié à"] ?: "",
|
||||
synonyme = line["Synonyme"] ?: "",
|
||||
antonyme = line["Antonyme"] ?: ""
|
||||
)
|
||||
|
||||
if (nouveauMot.nom.isNotBlank() && verifierChampsRequis(line)) {
|
||||
ajouterMotAuGlossaire(nouveauMot)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun verifierChampsRequis(mot: Map<String, String>): Boolean {
|
||||
//Vérifier que les headers sont égales aux champs requis
|
||||
|
@ -379,7 +456,7 @@ fun glossairePage(
|
|||
contentColor = Color.White
|
||||
)
|
||||
) {
|
||||
Text("Importer un fichier CSV")
|
||||
Text("Importer un fichier CSV ou XLSX")
|
||||
}
|
||||
|
||||
Button(
|
||||
|
@ -413,9 +490,10 @@ fun glossairePage(
|
|||
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun formulairePage(onAnnulerClick: () -> Unit) {
|
||||
// State to track whether to show the snackbar
|
||||
val snackbarVisibleState = remember { mutableStateOf(false) }
|
||||
|
||||
MaterialTheme {
|
||||
Column(
|
||||
|
@ -438,8 +516,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
onValueChange = { description.value = it },
|
||||
label = { Text("Description") },
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
focusedIndicatorColor = customRedColor, // Change this to the color you want
|
||||
unfocusedIndicatorColor = Color.Gray // Change this to the color you want
|
||||
focusedIndicatorColor = customRedColor,
|
||||
unfocusedIndicatorColor = Color.Gray
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -449,8 +527,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
onValueChange = { contextePrincipal.value = it },
|
||||
label = { Text("Contexte principal *") },
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
focusedIndicatorColor = customRedColor, // Change this to the color you want
|
||||
unfocusedIndicatorColor = Color.Gray // Change this to the color you want
|
||||
focusedIndicatorColor = customRedColor,
|
||||
unfocusedIndicatorColor = Color.Gray
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -460,8 +538,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
onValueChange = { contexte2.value = it },
|
||||
label = { Text("Contexte 2") },
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
focusedIndicatorColor = customRedColor, // Change this to the color you want
|
||||
unfocusedIndicatorColor = Color.Gray // Change this to the color you want
|
||||
focusedIndicatorColor = customRedColor,
|
||||
unfocusedIndicatorColor = Color.Gray
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -471,8 +549,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
onValueChange = { lieA.value = it },
|
||||
label = { Text("Lié à") },
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
focusedIndicatorColor = customRedColor, // Change this to the color you want
|
||||
unfocusedIndicatorColor = Color.Gray // Change this to the color you want
|
||||
focusedIndicatorColor = customRedColor,
|
||||
unfocusedIndicatorColor = Color.Gray
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -482,8 +560,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
onValueChange = { synonyme.value = it },
|
||||
label = { Text("Synonyme") },
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
focusedIndicatorColor = customRedColor, // Change this to the color you want
|
||||
unfocusedIndicatorColor = Color.Gray // Change this to the color you want
|
||||
focusedIndicatorColor = customRedColor,
|
||||
unfocusedIndicatorColor = Color.Gray
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -493,8 +571,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
onValueChange = { antonyme.value = it },
|
||||
label = { Text("Antonyme") },
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
focusedIndicatorColor = customRedColor, // Change this to the color you want
|
||||
unfocusedIndicatorColor = Color.Gray // Change this to the color you want
|
||||
focusedIndicatorColor = customRedColor,
|
||||
unfocusedIndicatorColor = Color.Gray
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -516,7 +594,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
onClick = {
|
||||
// Validation du formulaire
|
||||
if (nom.value.isBlank() || contextePrincipal.value.isBlank()) {
|
||||
println("Les champs 'Mot' et 'Contexte principal' sont obligatoires.")
|
||||
// Show the snackbar when validation fails
|
||||
snackbarVisibleState.value = true
|
||||
return@Button
|
||||
}
|
||||
|
||||
|
@ -552,10 +631,24 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
|
|||
}
|
||||
}
|
||||
|
||||
// Show the snackbar when the state is true
|
||||
if (snackbarVisibleState.value) {
|
||||
Snackbar(
|
||||
modifier = Modifier.padding(16.dp),
|
||||
action = {
|
||||
Button(onClick = { snackbarVisibleState.value = false }) {
|
||||
Text("OK")
|
||||
}
|
||||
}
|
||||
) {
|
||||
Text("Les champs 'Mot' et 'Contexte principal' sont obligatoires.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun chargerDonneesDepuisFichier(): List<Mot> {
|
||||
//if file is empty, return empty list
|
||||
if (!File("glossaire.json").exists() || File("glossaire.json").length() == 0L) {
|
||||
|
|
Loading…
Reference in New Issue