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, ";");
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 '€'.
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 '€'.
So the main question is how to encode file to "UTF-8 WITH BOM"?
UPDATE
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>
And I set toPrint = €€€ ££££ €£
for preview. PDF work fine but when I save file to CSV I see "€€€ ££££ €£"