Skip to content

Instantly share code, notes, and snippets.

@saecmca
Created March 29, 2024 12:47
Show Gist options
  • Save saecmca/d6af7627c19918d4e71b67814e022955 to your computer and use it in GitHub Desktop.
Save saecmca/d6af7627c19918d4e71b67814e022955 to your computer and use it in GitHub Desktop.
******************Main fun************
val excelFilePath = "/storage/emulated/0/Android/media/com.example.overlay/Overlay/LabelMaster.xlsx"
val outputXmlFilePath = "/storage/emulated/0/Android/media/com.example.overlay/Overlay/strings.txt"
val workbook: Workbook = readExcel(excelFilePath)//XSSFWorkbook(File(excelFilePath))
val sheet: Sheet = workbook.getSheetAt(0)
val stringResources = mutableMapOf<String, String>()
for (row in sheet) {
val key = row.getCell(0)?.stringCellValue ?: continue
var value = row.getCell(3)?.stringCellValue ?: continue
stringResources[key] = value
}
writeXml(stringResources, outputXmlFilePath)
workbook.close()
**************************
fun readExcel(filePath: String): XSSFWorkbook {
val file = File(filePath)
val fileInputStream = FileInputStream(file)
return XSSFWorkbook(fileInputStream)
}
fun writeXml(stringResources: Map<String, String>, filePath: String) {
val xmlBuilder = StringBuilder()
xmlBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
xmlBuilder.append("<resources>\n")
var i=0
for ((key, value) in stringResources) {
Log.w("val","$i=$key=$value")
i++
xmlBuilder.append(" <string name=\"$key\">$value</string>\n")
}
xmlBuilder.append("</resources>")
val file = File(filePath)
FileOutputStream(file).use { outputStream ->
outputStream.write(xmlBuilder.toString().toByteArray())
}
println("XML file generated successfully.")
}
implementation('org.apache.poi:poi:5.2.5')
implementation('org.apache.poi:poi-ooxml:5.2.5')
implementation("xerces:xercesImpl:2.12.1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment