Skip to content

Repository files navigation

XlsToJson

This sample implementation convert XLS to JSON

alt text

alt text

Convert JSON STRING to EXCEL File

@SuppressLint("SdCardPath", "SetTextI18n")
privatefunrunConvertJsonToXls(s: String) {
try {
customers = ConvertJsonToExcel.convertJsonString2Objects(s)
} catch (e: Exception) {
pathFileText!!.text = e.message
}
jsonStringData!!.setText(customers.toString())
pathExcel = "/storage/emulated/0/Download/customers.xlsx"try {
ConvertJsonToExcel.writeObjects2ExcelFile(customers!!, pathExcel)
pathFileText!!.text = "Success generate and save to : $pathExcel"jsonStringData!!.setText(jsonStr)
openExcelFile!!.visibility = View.VISIBLE
} catch (e: IOException) {
e.printStackTrace()
pathFileText!!.text = e.messagejsonStringData!!.setText(jsonStr)
}
}

Convert EXCEL File to JSON STRING

overridefunonRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 1000) {
try {
openSomeActivityForResult()
} catch (e: Exception) {
pathFileText!!.text = e.message
}
}
}
privatefuntaskConvertToJson() {
if (customers!!.isNotEmpty()) {
try {
// Step 2: Convert Java Objects to JSON StringvaljsonString = ConvertExcelToJson.convertObjects2JsonString(customers)
println(jsonString)
showJsonToView(jsonString)
} catch (e: Exception) {
e.printStackTrace()
pathFileText!!.text = e.message
}
}
}
privatevarsomeActivityResultLauncher = registerForActivityResult(
StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
valdata = result.data// There are no request codesvalfilePath = ArrayList<String?>()
if (data != null) {
if (data.clipData != null) {
for (indexin0untildata.clipData!!.itemCount) {
valitem = data.clipData!!.getItemAt(index)
filePath.add(convertUriToFilePath(applicationContext, item.uri))
}
} else {
filePath.add(convertUriToFilePath(applicationContext, data.data!!))
}
}
pathFileText!!.text = filePath[0]
try {
// Step 1: Read Excel File into Java List Objectscustomers = ConvertExcelToJson.readExcelFile(
filePath[0]
)
taskConvertToJson()
} catch (e: Exception) {
e.printStackTrace()
pathFileText!!.text = e.message
}
}
}
privatefunopenSomeActivityForResult() {
someActivityResultLauncher.launch(pick)
}

License

Copyright 2021 indosw
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages