java - Jasperreport CSV UTF-8 without BOM instead of UTF-8 -


i try export csv file jasperreport, problem when want print currency '€'.

when search solution, realized it's file encoding! write code!

//jasperprint 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 jasperreport encoded on "utf-8 without bom". when open file excel '€' looks '¬â'. when open file notepad++ '€' looks '€'.

on notepad++, convert file encoding utf-8 (with bom think), save file. open file excel , ---eureka---, '€' looks '€'.

so main question how encode file "utf-8 bom"?

update

i try 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 set toprint = €€€ ££££ €£ preview. pdf work fine when save file csv see "€€€ ££££ €£"

instead of resolving encode problem consider use different notation

writing code contains special characters, bad practice (switching of encoding on output file or compiling code using compiler expects different encoding ecc.), corrupt result

any character encoded utf-8 can represented using 4-digits hexadecimal code

the € u+20ac

so instead of putting can consider put \u20ac in jrxml code.

example

<textfield>     <reportelement x="0" y="0" width="100" height="25" uuid="bc2ae040-f9af-4732-82fe-8fe8b71696bd"/>     <textfieldexpression><![cdata["\u20ac"]]></textfieldexpression> </textfield> 

edit: after comment "but, value want print not static value", convert value unicode:

example of java code

public static string getasunicode(string value){     if (value==null){         return null;     }     string ret = "";     (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);   } 

and in jrxml call method:

<textfield>     <reportelement x="0" y="0" width="100" height="25" uuid="bc2ae040-f9af-4732-82fe-8fe8b71696bd"/>     <textfieldexpression><![cdata[myclass.getasunicode($p{toprint})]]></textfieldexpression> </textfield> 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -