不含BOM的Jasperreport CSV UTF-8而不是UTF-8 [英] Jasperreport CSV UTF-8 without BOM instead of UTF-8
问题描述
我尝试使用JasperReport导出CSV文件,问题是当我想打印€"之类的货币时.
I try to export a CSV file with JasperReport, the problem is when I want to print currency like '€'.
当我寻找解决方案时,我意识到这与文件编码有关!我写这段代码!
When I search for solution, I realized that it's about file encoding! I write this code!
//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打开文件时,€"看起来像¬â".但是当我用记事本++打开文件时,€"看起来像€".
The file exported by JasperReport is encoded on "UTF-8 without BOM". So when I open the file with Excel '€' looks like '¬â'. But When I open the file with Notepad++ '€' looks like '€'.
在Notepad ++上,我将文件编码转换为UTF-8(我认为是BOM),然后保存文件.我使用Excel和--- EUREKA ---打开文件,€"看起来像€".
On Notepad++, I convert file encoding to UTF-8 (with BOM I think), the I save the file. I open the file with Excel and ---EUREKA---, '€' looks like '€'.
所以主要问题是如何将文件编码为带BOM的UTF-8"?
So the main question is how to encode file to "UTF-8 WITH BOM"?
更新
我尝试这个jrxml
I try this 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时,会看到“££££££â€"â€"
And I set toPrint = €€€ ££££ €£
for preview. PDF work fine but when I save file to CSV I see "€€€ ££££ €£"
推荐答案
搜索后,我发现了一个解决方案:使用 cp1252 编码,请注意€"符号!所以最终的代码如下!
After Search, I found one solution : use cp1252 encoding, it take care the '€' symbol! so the final code is bellow!
//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, ";");
这篇关于不含BOM的Jasperreport CSV UTF-8而不是UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!