简体   繁体   English

如何在Java中使用POI-XSSF在Excel文件中插入表情符号

[英]How to insert emoji in Excel file using POI-XSSF in Java

I am using below code, for inserting emojis into excel using apache POI-HSSF, Please let me know how can I insert emojis into .xlsx file using POI-XSSF in Java, 我正在使用以下代码,以便使用apache POI-HSSF将表情符号插入excel,请让我知道如何使用Java中的POI-XSSF将表情符号插入.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());
}

Any help would be highly appreciated. 任何帮助将不胜感激。

You need to move on from xmlbeans-2.6.0 to higher version. 您需要从xmlbeans-2.6.0升级到更高版本。 I did replace it with 3.1.0 plus your UTF-8 needs to be enforced. 我确实用3.1.0替换了它,还需要执行您的UTF-8。 This step is not necessary but rather ensuring 此步骤不是必需的,而是确保

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(""));
                    }

Do not forget this it does not get lost in transformation for HttpServletResponse response : 不要忘记这一点,它不会在HttpServletResponse响应的转换中迷失方向:

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + filename + ";");

By the way, here is the change log for xmlbeans 2.6.0 vs 3.1.0 顺便说一下,这是xmlbeans 2.6.0与3.1.0的更改日志

  1. XMLBEANS-517: use safe XML parsers XMLBEANS-517:使用安全的XML解析器

    XMLBEANS-516: remove unnecessary javax and org.w3c classes XMLBEANS-516:删除不必要的javax和org.w3c类

    XMLBEANS-515: remove piccolo support XMLBEANS-515:删除短笛支持

    XMLBEANS-514: make java 6 the lowest supported runtime XMLBEANS-514:使Java 6成为受支持的最低运行时

    XMLBEANS-489: fix for Cursor getAllNamespaces not returning default namespace XMLBEANS-489:修复了Cursor getAllNamespaces不返回默认名称空间的问题

    Fix for XMLBEANS-499: xmlbeans2.6.0.jar contains duplicate class files (causes issues on Android) 修复XMLBEANS-499:xmlbeans2.6.0.jar包含重复的类文件(导致Android上的问题)

    XMLBEANS-447: Drop the ConcurrentReaderHashMap source code XMLBEANS-447:删除ConcurrentReaderHashMap源代码

    Fix for XMLBEANS-404: entitizeContent CDATA loop iterating too many times (causes assertion error or ArrayIndexOutOfBoundsException in replace) 修复XMLBEANS-404:entitizeContent CDATA循环重复太多次(在替换中导致断言错误或ArrayIndexOutOfBoundsException)

    Fix for XMLBEANS-332: XMLBeans changes surrogate pair bytes to question marks 修复XMLBEANS-332:XMLBeans将代理对字节更改为问号

This is working since xmlbeans 3.0.0 从xmlbeans 3.0.0开始这是有效的

Someone mentioned version 2.6.2, which is not indexed anymore (as of now, June 2019), so jump directly to 3.xx 有人提到2.6.2版本,该版本已不再编入索引(截至2019年6月),因此请直接跳至3.xx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM