我试着用JasperReport导出一个CSV文件,问题是当我想打印“欧元”这样的货币时。
当我寻找解决方案时,我意识到这是关于文件编码的!这是我写的代码!
//JasperPrint is already filled
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.setContentType("application/csv; charset="+Charset.forName("utf-8").displayName());
httpServletResponse.setCharacterEncoding(Charset.forName("utf-8").displayName());
httpServletResponse.addHeader("Content-disposition", "attachment; filename=nameoffile.csv");
httpServletResponse.addHeader("Content-type", "application/csv; charset="+Charset.forName("utf-8").displayName());
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, Charset.forName("utf-8").displayName());
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
exporter.setParameter(JRCsvExporterParameter.CHARACTER_ENCODING, Charset.forName("utf-8").displayName());
exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, ";");由JasperReport导出的文件是在"UTF-8 而没有BOM“上编码的。所以,当我用Excel打开文件时,‘欧元’看起来像‘.’‘。但是当我用Notepad++打开文件时,‘欧元’看起来就像‘欧元’。
在Notepad++上,我将文件编码转换为UTF-8 (使用BOM,我认为),我保存文件。我用Excel打开文件--尤里卡--“欧元”看起来像“欧元”。
因此,主要的问题是如何将文件编码为"UTF-8与BOM"?
更新
我尝试这个jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report2" language="groovy" pageWidth="100" pageHeight="842" columnWidth="100" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="b7ec44fd-90d0-4ecc-8f99-0e5eafc16828">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="toPrint" class="java.lang.String"/>
<title>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="d2c55a11-b407-407b-b117-3b04d20cccec"/>
<textFieldExpression><![CDATA[$P{toPrint}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>我将toPrint = €€€ ££££ €£设置为预览。PDF工作正常,但当我将文件保存到CSV时,我看到“-”
发布于 2016-01-28 07:22:36
经过搜索,我找到了一个解决方案:使用cp1252编码,它小心‘欧元’符号!所以最后的代码如下!
//JasperPrint is already filled
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.setContentType("application/csv; charset=cp1252");
httpServletResponse.setCharacterEncoding("cp1252");
httpServletResponse.addHeader("Content-disposition", "attachment; filename=nameoffile.csv");
httpServletResponse.addHeader("Content-type", "application/csv; charset="+Charset.forName("utf-8").displayName());
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "cp1252");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
exporter.setParameter(JRCsvExporterParameter.CHARACTER_ENCODING, "cp1252");
exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, ";");发布于 2016-01-27 13:39:26
您可以考虑使用不同的符号,而不是解决您的编码问题
编写包含特殊字符的代码通常是错误的做法(在输出文件上切换编码或使用需要不同编码ecc的编译器编译代码。),这将破坏结果。
任何用UTF-8编码的字符只能使用它的4位十六进制代码来表示。
欧元是U+20AC
因此,您可以考虑将€放在jrxml代码中,而不是放在jrxml代码中。
示例
<textField>
<reportElement x="0" y="0" width="100" height="25" uuid="bc2ae040-f9af-4732-82fe-8fe8b71696bd"/>
<textFieldExpression><![CDATA["\u20AC"]]></textFieldExpression>
</textField>编辑:在注释“但是,我要打印的值不是静态值”之后,将值转换为unicode:
java代码示例
public static String getAsUnicode(String value){
if (value==null){
return null;
}
String ret = "";
for (char ch : value.toCharArray()) {
ret += getUnicodeEscaped(ch);
}
return ret;
}
public static String getUnicodeEscaped(char ch) {
if (ch < 0x10) {
return "\\u000" + Integer.toHexString(ch);
} else if (ch < 0x100) {
return "\\u00" + Integer.toHexString(ch);
} else if (ch < 0x1000) {
return "\\u0" + Integer.toHexString(ch);
}
return "\\u" + Integer.toHexString(ch);
}在jrxml中,调用您的方法:
<textField>
<reportElement x="0" y="0" width="100" height="25" uuid="bc2ae040-f9af-4732-82fe-8fe8b71696bd"/>
<textFieldExpression><![CDATA[MyClass.getAsUnicode($P{toPrint})]]></textFieldExpression>
</textField>https://stackoverflow.com/questions/35037404
复制相似问题