有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ApachePOI如何在Java中使用POIXSSF在Excel文件中插入表情符号

我使用下面的代码,使用ApachePOI-HSSF将表情符号插入excel,请告诉我如何将表情符号插入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());
}

任何帮助都将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    您需要从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的更改日志

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

      XMLBEANS-516:删除不必要的javax和org。w3c课程

      XMLBEANS-515:删除piccolo支持

      XMLBEANS-514:使java 6成为受支持的最低运行时

      XMLBEANS-489:修复游标getAllNamespaces不返回默认值 名称空间

      修复XMLBEANS-499:xmlbeans2。6.0.jar包含重复的类 文件(导致安卓系统出现问题)

      XMLBEANS-447:删除ConcurrentReaderHashMap源代码

      修复了XMLBEANS-404:entitizeContent CDATA循环迭代过多的问题 次数(在中导致断言错误或ArrayIndexOutOfBoundsException) 替换)

      XMLBEANS-332的修复:XMLBEANS将代理项对字节更改为 问号

  2. # 2 楼答案

    自xmlbeans 3.0.0以来,它一直在工作

    有人提到版本2.6.2,它不再被索引(截至现在,2019年6月),所以直接跳到3。x、 x