Hot questions for Using JasperReports in csv

Question:

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 "€€€ ££££ €£"


Answer:

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, ";");

Question:

I export CSV file through jasper-reports. It works fine however I want switch comma to semicolon. I found some solution but it is deprecated. I was looking for a method in SimpleCsvReportConfiguration without happy ending. Can someone help?

JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrints));
exporter.setExporterOutput(new SimpleWriterExporterOutput(response.getOutputStream()));
SimpleCsvReportConfiguration configuration = new SimpleCsvReportConfiguration();
exporter.exportReport();

Answer:

You should add it to your .jrxml file:

<property name="net.sf.jasperreports.export.csv.field.delimiter" value=";"/>

Question:

I would like to export CSV file with JasperReports. I am using REST and I do not know how to set ExporterOutput. It works with PDF/XLSX however I can not get the solution of CSV.

JasperPrint jasperPrint = storageManager.generateYearAggregateXLS(id, year);
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment;filename=Agregaty.xlsx");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");

    JRCsvExporter exporter = new JRCsvExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));

    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));

    SimpleCsvReportConfiguration configuration = new SimpleCsvReportConfiguration();
    exporter.setConfiguration(configuration);
    exporter.exportReport();

In exporter.setExporterOutput I have followin problem: The method setExporterOutput(WriterExporterOutput) in the type JRAbstractExporter<CsvReportConfiguration,CsvExporterConfiguration,WriterExporterOutput,JRCsvExporterContext> is not applicable for the arguments (SimpleOutputStreamExporterOutput)

It works with excel perfectly.


Answer:

I found solution and it is really simple:

exporter.setExporterOutput(new SimpleWriterExporterOutput(response.getOutputStream()));

I've changed writer OutputStreamExporterOutput from SimpleOutputStreamExporterOutput to SimpleWriterExporterOutput

Question:

I have a report which I need to export to pdf and xls files. I am trying to save the report in the JasperViewer without explicitly writing code for exporting in my Java class. The problem is that when I try to save in the above manner, the pdf and csv are getting created correctly with the data populated but the xls seems to be just blank. I am using Ubuntu 14.04 ,but however I don't think that would be an issue as xls formats can still be opened and viewed on Linux.

Below is my jrxml file

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2015-12-22T12:52:53 -->
<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="_blank" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ced95029-b569-4027-895c-af2843a4e088">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="net.sf.jasperreports.export.xlsx.detect.cell.type" value="true"/>
    <style name="Title" fontName="Times New Roman" fontSize="50" isBold="true"/>
    <style name="SubTitle" forecolor="#E50000" fontName="Arial" fontSize="18"/>
    <style name="Column header" forecolor="#666666" fontName="Times New Roman" fontSize="12" isBold="true"/>
    <style name="Detail" fontName="Times New Roman" fontSize="12"/>
    <style name="Row" mode="Opaque" fontName="Times New Roman" fontSize="12">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style backcolor="#000000"/>
        </conditionalStyle>
    </style>
    <parameter name="username" class="java.lang.String"/>
    <field name="filePath" class="java.lang.String"/>
    <field name="createdOn" class="java.sql.Timestamp"/>
    <field name="actionby" class="java.lang.Integer"/>
    <field name="actionType" class="java.lang.String"/>
    <sortField name="createdOn"/>
    <title>
        <band height="136" splitType="Stretch">
            <staticText>
                <reportElement style="Title" x="170" y="0" width="230" height="60" uuid="1a652692-054a-40f3-ade5-68d8da36626d"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Report]]></text>
            </staticText>
            <textField>
                <reportElement style="SubTitle" x="219" y="62" width="300" height="22" uuid="88d4231e-f014-4b0d-b871-1737ba63a1ae"/>
                <textElement>
                    <font fontName="Times New Roman"/>
                </textElement>
                <textFieldExpression><![CDATA["Activity Report of user "+$P{username}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="172" y="94" width="383" height="42" uuid="f0bfb1ea-a85a-47cd-a1a4-43f22b45e80f"/>
                <textElement textAlignment="Right">
                    <font size="10"/>
                </textElement>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="16" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="15" width="555" height="1" uuid="1a61a836-d137-48b1-ad67-6ff64600bf93"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
            <staticText>
                <reportElement style="Column header" x="20" y="0" width="100" height="15" forecolor="#03A9F4" uuid="8093e6b6-1479-4d7e-a3e1-8b43ac16fe34"/>
                <text><![CDATA[Date]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="150" y="0" width="250" height="15" forecolor="#03A9F4" uuid="c9463f8f-ce24-4798-9f83-1122da0d5f31"/>
                <text><![CDATA[File/Folder modified]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="400" y="0" width="150" height="15" forecolor="#03A9F4" uuid="cfb1f8c6-3240-4393-be47-70b56eea3926"/>
                <text><![CDATA[Action Taken]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="16" splitType="Stretch">
            <frame>
                <reportElement style="Row" mode="Transparent" x="0" y="0" width="555" height="15" forecolor="#009688" uuid="a98495d8-3c8d-4fa0-81f5-30c3efc2f766"/>
                <textField isStretchWithOverflow="true" pattern="dd-MMM-yyyy">
                    <reportElement style="Detail" x="20" y="0" width="100" height="15" forecolor="#009688" uuid="714380ba-f273-4f2c-9a24-e01f345aeb03"/>
                    <textFieldExpression><![CDATA[$F{createdOn}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="150" y="0" width="250" height="15" forecolor="#009688" uuid="e7d518fc-f335-40ab-acc1-8858f53a806e"/>
                    <textFieldExpression><![CDATA[$F{filePath}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="400" y="0" width="150" height="15" forecolor="#009688" uuid="34a6eb4a-7c05-4129-815e-ae1153f52798"/>
                    <textFieldExpression><![CDATA[$F{actionType}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1" uuid="fa45a887-615a-4d84-a2d7-8a2219671b3c"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="25" splitType="Stretch">
            <frame>
                <reportElement mode="Transparent" x="-21" y="1" width="597" height="24" forecolor="#D0B48E" backcolor="#F2EBDF" uuid="183682bc-d976-4756-83e0-6625a3f98ed1"/>
                <textField evaluationTime="Report">
                    <reportElement style="Column header" x="533" y="0" width="40" height="20" forecolor="#03A9F4" uuid="0193f9b3-1559-491a-8580-b6988863b6a1"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement style="Column header" x="453" y="0" width="80" height="20" forecolor="#03A9F4" uuid="d00b105e-494b-418b-8ac9-8b1b4824f4f0"/>
                    <textElement textAlignment="Right" verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                </textField>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement style="Column header" x="22" y="1" width="197" height="20" forecolor="#03A9F4" uuid="0616f3fe-0354-456f-8911-ec30ec51a5ae"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

Below is the snippet of my Java code dealing with the report part

java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("com/ram/report/jasper/Report.jrxml");
                    JasperReport jasperReport=JasperCompileManager.compileReport(in);
                    HashMap<String, Object> params = new HashMap<String, Object>();
                    try
                    {
                        if(emp!=null)
                            params.put("username", emp.getFirstName()+" "+emp.getLastName());
                        else
                            params=null;
                    }
                    catch(Exception e)
                    {
                        params=null;
                    }
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,new JRBeanCollectionDataSource(list));
                    JasperViewer.viewReport(jasperPrint,false);                 
                    System.out.println("Wrote report");
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

I went through this link but couldn't understand the workaround to be done. Please help!


Answer:

Most probably you missing Apache POI from your libraries project (if you use ant) or from you maven repo - pom.xml (if you use maven).

I don't see any error to your code