简体   繁体   English

不含BOM的Jasperreport CSV UTF-8而不是UTF-8

[英]Jasperreport CSV UTF-8 without BOM instead of UTF-8

I try to export a CSV file with JasperReport, the problem is when I want to print currency like '€'. 我尝试使用JasperReport导出CSV文件,问题是当我想打印“€”之类的货币时。

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 ". JasperReport导出的文件在“ 没有BOM的 UTF-8”上编码。 So when I open the file with Excel '€' looks like '¬â'. 因此,当我使用Excel打开文件时,“€”看起来像“¬â”。 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. 在Notepad ++上,我将文件编码转换为UTF-8(我认为是BOM),然后保存文件。 I open the file with Excel and ---EUREKA---, '€' looks like '€'. 我使用Excel和--- EUREKA ---打开文件,“€”看起来像“€”。

So the main question is how to encode file to "UTF-8 WITH BOM"? 因此,主要问题是如何将文件编码为“ UTF-8 WITH BOM”?

UPDATE UPDATE

I try this jrxml 我尝试这个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. 我设置为toPrint = €€€ ££££ €£预览。 PDF work fine but when I save file to CSV I see "€€€ ££££ €£" PDF可以正常工作,但是当我将文件保存为CSV时,我会看到““££££££â€”

Instead of resolving your encode problem you could consider to use a different notation 除了解决编码问题外,您可以考虑使用其他符号

Writing code that contains special characters, is normally bad practice (switching of encoding on output file or compiling code using a compiler that expects a different encoding ecc.), will corrupt the result 编写包含特殊字符的代码通常是一种不好的做法(在输出文件上切换编码或使用期望使用不同编码ecc的编译器来编译代码)会破坏结果。

Any character encoded with UTF-8 can be represented using only its 4-digits hexadecimal code 使用UTF-8编码的任何字符都只能使用其4位十六进制代码表示

The € is U+20AC €为U + 20AC

So instead of putting you can consider to put \€ in your jrxml code. 因此,您可以考虑在\€ jrxml代码中添加\€ ,而不是添加

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, the value i want to print is not a static value" , convert the value to unicode: 编辑 :在注释“但是,我要打印的值不是静态值”之后将该转换为unicode:

Example of java code 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);
  }

and in jrxml call your method: 并在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>

After Search, I found one solution : use cp1252 encoding, it take care the '€' symbol! 搜索后,我发现了一个解决方案:使用cp1252编码,请注意“€”符号! 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, ";");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM