java - 如何在java中转换em破折号

标签 java utf-8 apache-poi

I have a problem, where when the end user submits the data from HTML form in a web application, they are copying the data from Word document which contains long dash or em dash.

As per the logic we are trying to read those data from database and writing it to an excel file.

As an outcome those characters are generated in the excel as shown below, which contains a kind of question mark.

  Actual output : 1993 � 1995
Expected output : 1993 – 1995 

我已经在Java中完成了UTF-8编码,但在Excel中仍然得到相同的输出。怎么解决这个问题?

下面是我的代码摘录。

try {
        keyStrenghts = new String(keyStrenghts.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

我使用 JDK 6 和 apache poi 来生成 Excel 文件。

最佳答案

如果仅限于破折号,这可能会解决您的问题:

keyStrenghts = keyStrenghts.replaceAll("\\p{Pd}", "-");

这是使用正则表达式将所有破折号替换为ascii“-”,如here所述。

关于java - 如何在java中转换em破折号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549489/

相关文章:

php - 如何通过 PHPExcel 在 Excel 中读取德语字符 (äöü߀)?

Java:Apache Poi 的 Excel 到 csv 日期转换问题

java - 将 int 转换为字节数组并使用 System.out.write 方法打印到控制台

java - 为什么我们在 Java 中需要数字文字?

java - 带有空 xml 文档的 Jdom2 解析器

java - 努力使用 log4j2 和 SMTP Appender 发送电子邮件

c - 使用 iconv() 的 UTF-8 到 C/POSIX 语言环境转换失败

Python utf-8 字符范围

grails - 导入具有小行高的Excel文件(在错误的位置找到行/单元格记录)

java - XSSF POI 是单元格日期