Dear Support Team,
I am currently facing a problem,
When I try to convert a JSON string that contains different unicodes (\u20ac => €), the .xlsx or .csv output gets corrupted.
I have workaround with a simple replace all statement which is not really a good solution I want to avoid big regular expressions.
I wonder if there is a better solution like just set the encoding correctly to utf-8 or something and handle all unicode with one line of code. Please let me now, thanks a lot.
Best regards and thanks for your help!
JSON sample:
[
{
“PLANBUDGET_FORMATIERT”: “544,00 \u20ac”,
“CVR”: 1.23
}
]
Java Code snippet:
public void generateCellsByteArray(boolean replaceUnicode, String fileName) {
String json = null;
String jsonReplacementData = null;
Workbook workbook = null;
Worksheet worksheet= null;
try {
Locale.setDefault(Locale.GERMANY);
workbook = new Workbook(FileFormatType.XLSX);
worksheet = workbook.getWorksheets().get(0);
json = getJson(“source.json”);
// Is there a better way to handle unicodes and display it correctly?
// Encoding encoding = Encoding.getUTF8(); // can i use this in some way?
// current workaround
if(json.contains("\u20ac") && replaceUnicode) {
jsonReplacementData = json.replaceAll("\\u20ac", “€”);
System.out.println("Replaced json: \n "+ jsonReplacementData);
} else {
jsonReplacementData = json;
}
// import json data to default worksheet starting at cell A1
JsonUtility.importData(jsonReplacementData, worksheet.getCells(), 0, 0, getLayoutOptions());
// Text alignment
handleCellStyle(worksheet, workbook);
// save file
workbook.save(getSaveFilePath(fileName, FileFormatUtil.saveFormatToExtension(FileFormatType.XLSX)), FileFormatType.XLSX);
} catch (Exception e) {
e.printStackTrace();
}
}
Sample project:
example_unicode.zip (35.7 KB)