使用带有中文字符的 poi 在 java 中写入 .xlsx 文件

write .xlsx file in java using poi with chinese characters

我正在尝试使用 apache poi 创建 .xlsx 文件。它可以很好地处理野蛮人和印地语等字符。但是我在我的应用程序中也使用了汉字。当我使用我的代码创建 .xlsx 文件时,只显示中文字符。我正在使用此代码创建 .xlsx 文件。

Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("new sheet");
        // Create a row and put some cells in it. Rows are 0 based.
        Row row = sheet.createRow(1);
        // Create a cell and put a value in it.
        Cell cell = row.createCell(1);
        //arbaric char set in cell and show successfully in .xlsx
        cell.setCellValue("سلام");
        Cell cell2 =    row.createCell(2);
        **//chinese char set in cell and not show in .xlsx**
        cell2.setCellValue(" 邓敏 ");
        Cell cell3 = row.createCell(3);
        //hindi char set in cell here and show successfully in .xlsx
        cell3.setCellValue("हिन्दी");
        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook5.xlsx");
        wb.write(fileOut);
        fileOut.close();

任何人都可以帮助我如何使用 apache poi 库在 .xlsx 文件中写入汉字。下面我提前附上.xlsx文件output.Thanks

Apache POI 中存在一些与 Unicode 中所谓的 "surrogate pairs" 相关的已知错误,请参阅 https://bz.apache.org/bugzilla/show_bug.cgi?id=58247 and https://bz.apache.org/bugzilla/show_bug.cgi?id=54084

不幸的是,修复它并不容易,因为它发生在另一个库的深处。你可以试试其他的汉字,如果有的能用,有的不能用,那很有可能是同样的问题。

已修复第三方库,请参阅 https://bz.apache.org/bugzilla/show_bug.cgi?id=59268, you can try to replace your XMLBeans-jar with the one from http://mvnrepository.com/artifact/com.github.pjfanning/xmlbeans 看看是否能解决问题。如果您尝试,请对错误发表评论,以便我们知道它有效!