我使用下面的代码,使用apache POI-HSSF将emojis插入excel,请告诉我如何使用Java中的POI-XSSF将emojis插入.xlsx文件,
Workbook workBook =new HSSFWorkbook();
Sheet createSheet = workBook.createSheet("Emoji");
String str =""+"somevalue";
Row createRow = createSheet.createRow(0);
createRow.createCell(0).setCellValue(str);
//creating a file and writing the Workbook data
try {
FileOutputStream fileOutputStream = new FileOutputStream("/tmp/MyFirstExcel.xls");
workBook.write(fileOutputStream);
fileOutputStream.close();
} catch (IOException iException) {
System.out.println("IO exception occured while creating the file" + iException.getMessage());
}
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
您需要从xmlbeans-2.6.0升级到更高版本。我确实将其替换为3.1.0 加上您的UTF-8需要执行。此步骤不是必需的,而是可以确保
String cleanedText = StringEscapeUtils.unescapeJava(yourstringhere);
byte[] bytes = cleanedText.getBytes(StandardCharsets.UTF_8);
String text = new String(bytes, StandardCharsets.UTF_8);
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
CharsetEncoder encoder = charset.newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
try {
// The new ByteBuffer is ready to be read.
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(text));
// The new ByteBuffer is ready to be read.
CharBuffer cbuf = decoder.decode(bbuf);
String str = cbuf.toString();
RichTextString rx = createHelper.createRichTextString(str);
row.createCell((short) 1).setCellValue(rx);
} catch (CharacterCodingException e) {
logger.error("PUT SOME CODE HERE FOR DEBUGGING");
row.createCell((short) 1).setCellValue(new XSSFRichTextString(""));
}
不要忘记,它不会在HttpServletResponse响应的转换中迷失方向:
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ";");
顺便说一下,这是xmlbeans 2.6.0与3.1.0的更改日志
XMLBEANS-517:使用安全的XML解析器
XMLBEANS-516:删除不必要的javax和org.w3c类
XMLBEANS-515:删除短笛支持
XMLBEANS-514:使Java 6成为受支持的最低运行时
XMLBEANS-489:修复了Cursor getAllNamespaces不返回默认值的问题 命名空间
修复XMLBEANS-499:xmlbeans2.6.0.jar包含重复的类 文件(导致Android设备出现问题)
XMLBEANS-447:删除ConcurrentReaderHashMap源代码
修复XMLBEANS-404:entitizeContent CDATA循环迭代太多 次(导致断言错误或ArrayIndexOutOfBoundsException在 替换)
修复XMLBEANS-332:XMLBeans将代理对字节更改为 问号
答案 1 :(得分:0)
此功能自xmlbeans 3.0.0起
有人提到2.6.2版本,该版本已不再编入索引(截至2019年6月),因此请直接跳至3.x.x