我正在使用下面的代码,用于使用apache POI-HSSF将emoji插入到excel中,请告诉我如何使用Java中的POI-XSSF将emoji插入到.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());
}任何帮助都将不胜感激。
发布于 2019-06-19 07:20:02
您需要从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:使用安全的
移除不必要的javax和org.w3c类
XMLBEANS-515:删除piccolo支持
XMLBEANS-514:使java6成为最低支持的运行时
XMLBEANS-489:修复游标getAllNamespaces不返回默认命名空间的问题
修复XMLBEANS-499: xmlbeans2.6.0.jar包含重复的类文件(导致Android上的问题)
XMLBEANS447:删除ConcurrentReaderHashMap源代码
修复XMLBEANS-404: entitizeContent CDATA循环迭代次数过多(导致断言错误或替换中的ArrayIndexOutOfBoundsException )
修复XMLBEANS-332: XMLBeans将代理项对字节更改为问号
发布于 2019-06-26 12:23:56
这是从xmlbeans 3.0.0开始工作的
有人提到了2.6.2版本,它不再被索引(截至2019年6月),所以直接跳到3.x.x
https://stackoverflow.com/questions/46239082
复制相似问题