Update csv export
parent
a09b749133
commit
dfa960e968
|
@ -21,18 +21,14 @@ import androidx.compose.ui.ExperimentalComposeUiApi
|
|||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.window.*
|
||||
import database.DatabaseManager
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.IOException
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
|
||||
|
||||
val connection = DatabaseManager.getConnection()
|
||||
val customRedColor = Color(0xFFB70D1B)
|
||||
|
||||
@OptIn(ExperimentalComposeUiApi::class)
|
||||
|
@ -124,23 +120,24 @@ fun App() {
|
|||
}
|
||||
},
|
||||
onExporterClick = {
|
||||
val fileDialog = FileDialog(Frame(), "Save as", FileDialog.SAVE)
|
||||
fileDialog.file = "GlossaireExporte.csv"
|
||||
val fileDialog = FileDialog(Frame(), "Save as CSV", FileDialog.SAVE)
|
||||
fileDialog.file = "glossaire_exporte.csv" // Initial file name
|
||||
fileDialog.isVisible = true
|
||||
|
||||
val selectedFile = fileDialog.file
|
||||
val selectedDirectory = fileDialog.directory
|
||||
|
||||
if (selectedFile != null) {
|
||||
val csvFilePath = selectedDirectory + selectedFile
|
||||
val csvFilePath = "$selectedDirectory$selectedFile"
|
||||
val jsonFilePath = "glossaire.json" // Replace with the actual path to your JSON file
|
||||
|
||||
println("Exporting to: $csvFilePath")
|
||||
|
||||
exportJsonToCSV("glossaire.json", csvFilePath)
|
||||
exportToCSV(jsonFilePath, csvFilePath)
|
||||
} else {
|
||||
println("Export command cancelled by user.")
|
||||
}
|
||||
}
|
||||
,
|
||||
},
|
||||
onRetourClick = { currentPage.value = "accueil" }
|
||||
)
|
||||
}
|
||||
|
@ -208,48 +205,36 @@ fun App() {
|
|||
}
|
||||
}
|
||||
|
||||
fun exportJsonToCSV(jsonFilePath: String, csvFilePath: String) {
|
||||
fun exportToCSV(jsonFilePath: String, csvFilePath: String) {
|
||||
val glossary = readJsonFile(jsonFilePath)
|
||||
|
||||
val csvContent = buildString {
|
||||
appendLine("nom,description,contextePrincipal,contexte2,lieA,synonyme,antonyme")
|
||||
glossary.forEach { entry ->
|
||||
appendLine(
|
||||
"${entry["nom"]},${entry["description"]},${entry["contextePrincipal"]}," +
|
||||
"${entry["contexte2"]},${entry["lieA"]},${entry["synonyme"]},${entry["antonyme"]}"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Read JSON file
|
||||
val jsonContent = File(jsonFilePath).readText(Charsets.UTF_8)
|
||||
val glossaryData = Json.parseToJsonElement(jsonContent)
|
||||
|
||||
if (glossaryData is JsonObject) {
|
||||
val header = glossaryData.keys.toList()
|
||||
val csvContent = buildString {
|
||||
// Append the header
|
||||
append(header.joinToString(separator = ";"))
|
||||
append("\n")
|
||||
|
||||
// Append the data
|
||||
appendJsonToCSV(glossaryData, this)
|
||||
}
|
||||
|
||||
// Write to CSV file
|
||||
File(csvFilePath).writeText(csvContent, Charsets.UTF_8)
|
||||
println("Export successful. File saved at: $csvFilePath")
|
||||
} else {
|
||||
println("Invalid JSON file format.")
|
||||
FileWriter(csvFilePath).use { fileWriter ->
|
||||
fileWriter.write(csvContent)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
println("Error exporting data: ${e.message}")
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun appendJsonToCSV(json: JsonObject, stringBuilder: StringBuilder) {
|
||||
val values = json.values.map {
|
||||
when (it) {
|
||||
is JsonPrimitive -> it.content
|
||||
is JsonObject -> {
|
||||
val nestedValues = buildString { appendJsonToCSV(it, this) }
|
||||
"\"$nestedValues\""
|
||||
}
|
||||
else -> ""
|
||||
}
|
||||
fun readJsonFile(filePath: String): List<Map<String, String>> {
|
||||
return try {
|
||||
val content = File(filePath).readText(Charsets.UTF_8)
|
||||
Json.decodeFromString(content)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
emptyList()
|
||||
}
|
||||
|
||||
stringBuilder.append(values.joinToString(separator = ";"))
|
||||
stringBuilder.append("\n")
|
||||
}
|
||||
|
||||
fun importCSV(cheminFichier: String) {
|
||||
|
|
Loading…
Reference in New Issue