java – JasperReport报告中的Excel单元格格式

前端之家收集整理的这篇文章主要介绍了java – JasperReport报告中的Excel单元格格式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在研究生成Excel文件的JasperReport报告.出于某种原因,我的单元格格式/类型不是它们应该如何.例如,我在单元格中有Date对象,但是当我生成Excel文件时,它将单元格类型设置为Number,或者Long类型是单元格中的文本,但单元格的格式是数字,并且当用户编辑日期单元格时(例如,日期为11/02) / 2012更改为11/03/2012)它将日期转换为数字(41581.00).

这是我的代码(它只是将弹出流输出到带有报告的浏览器窗口):

public void generateXLSPopup(String tmpltFileLocation,Map<String,Object> params,Collection vo) {
    log.fine("ReportEngine: Start Generate XLS Popup Report Function!");

    Filename f = new Filename(tmpltFileLocation);

    String xlsFileName = f.getFileName() +  "_" + sDateFormated + ".xlsx";

    try {
        JasperPrint jasperPrint = getJRPrint(tmpltFileLocation,params,new JRBeanCollectionDataSource(vo));

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRXlsxExporter exporter = getCommonXlsxExporter();

        exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT,jasperPrint);
        exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,baos); // fill byte array output stream

        exporter.exportReport();

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment; filename=" + xlsFileName);
        response.setContentLength(baos.size());
        response.getOutputStream().write(baos.toByteArray());
        context.responseComplete();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    log.fine("ReportEngine: Finish Generate XLS Popup Report Function!");
}


private JRXlsxExporter getCommonXlsxExporter(){
    JRXlsxExporter exporter = new JRXlsxExporter();
    exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS,Boolean.TRUE);
    exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
    exporter.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE,Boolean.TRUE);
    exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
    exporter.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);
    //exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);


    return exporter;
}

以下是我的jasper报告xml文件中前几行的示例:

<textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement style="alternateStyle" stretchType="RelativeToBandHeight" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.Long"><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement style="alternateStyle" stretchType="RelativeToBandHeight" x="200" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{emsProdNo}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement style="alternateStyle" stretchType="RelativeToBandHeight" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{courseName}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="MMMMM dd,yyyy" isBlankWhenNull="true">
                <reportElement style="alternateStyle" stretchType="RelativeToBandHeight" x="300" y="0" width="98" height="20"/>
                <textElement>
                    <font isUnderline="true"/>
                </textElement>
                <textFieldExpression class="java.util.Date"><![CDATA[$F{startDate}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement style="alternateStyle" stretchType="RelativeToBandHeight" x="474" y="0" width="81" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{endDateStr}]]></textFieldExpression>
            </textField>

(请不要问我为什么我会动态生成jasperreport模板文件,这就是我需要的方式.)

解决方法

在新版本的JasperReports中引入了参数net.sf.jasperreports.export.xls.pattern.

例子:

<textField pattern="EEE,MMM d,yyyy">
    <reportElement x="100" y="12" width="75" height="11">
        <property name="net.sf.jasperreports.export.xls.pattern" value="ddd,mmm d,yyyy"/>
    </reportElement>
    <textElement textAlignment="Right"/>
    <textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{dateField}]]>
    </textFieldExpression>
</textField>

有关此参数的信息是here.使用的样本是here.

原文链接:https://www.f2er.com/java/127978.html

猜你在找的Java相关文章