Cannot read chinese characters in an excel file

356 Views Asked by At

I have developed a java application that will read an excel file containing chinese characters and convert it to multiple CSV files. The file is as below:

public class SplitterBean {

    public static final Logger LOGGER = LoggerFactory.getLogger(SplitterBean.class);

    public List<Map<String, String>> splitBody(XSSFWorkbook workbook) {
        LOGGER.info("Inside SplitterBean, splitting: " + workbook.getNumberOfSheets());

        Map<String, String> sheetMap = null;
        List<Map<String, String>> sheetList = new ArrayList<Map<String,String>>();
        int numberOfSheets = workbook.getNumberOfSheets();
        XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
        for (int i = 0; i < numberOfSheets; i++) {
            StringBuilder sb = new StringBuilder();
            sheetMap = new HashMap<String, String>();
            XSSFSheet sheet = workbook.getSheetAt(i);
            String sheetName = sheet.getSheetName();
            for (Row row : sheet) {
                for (Cell cell : row) {
                    String cellValue = null;
                    LOGGER.info("Cell type is: " + cell.getCellType());
                    switch (cell.getCellType()) {
                        case NUMERIC:
                            cellValue = Double.toString(cell.getNumericCellValue());
                            break;
                        case STRING:
                            cellValue = cell.getStringCellValue();
                            break;
                        case BLANK:
                            cellValue = "";
                            break;
                        case FORMULA:
                            LOGGER.info("Reached formula cell, cell type is: " + cell.getCellType().toString());
                            switch (cell.getCellType()) {
                                case NUMERIC:
                                    cellValue = Double.toString(cell.getNumericCellValue());
                                    break;
                                case STRING:
                                    cellValue = cell.getStringCellValue();
                                    break;
                                case BLANK:
                                    cellValue = "";
                                    break;
                                default:
                                    cellValue = "";
                                    break;
                            }
                        default:
                            cellValue = "";
                            break;
                    }
                    sb.append(cellValue).append(",");
                }
                sb.append("\n");
            }
            sheetMap.put(sheetName, sb.toString());
            sheetList.add(sheetMap);
        }
        return sheetList;
    }

}

We are deploying our project as war file in Wildfly 17.0.1.FINAL. But when the CSV file is generated, the chinese characters are getting distorted. I guess this is due to server encoding which I need to change to UTF-8. Can anyone suggest me how to change server encoding in Wildfly 17.0.1.FINAL or how this issue can be solved?

0

There are 0 best solutions below