将jasper报告导出为XLS格式时,不会发生单元扩展

时间:2016-01-28 06:40:59

标签: java jasper-reports export-to-excel

我有一个带有静态字段和文本字段的ireport。数据库由大部分数据组成,对于数据库中的单行,有多行数据。所以当我通过java代码将jasper报告导出为PDF格式时正在拉伸,精心设计和打印整个数据而不会丢失任何行,但是当我导出到XLS时,它会丢失数据,而且还没有发生单元格的拉伸。我将显示一个图像以便更好地理解。enter image description here

正如您在上面所看到的,数据在PDF中打印得非常清晰。 enter image description here

在上面,那是XLS格式数据隐藏或意义上缺失。所以我的问题是当报表导出为XLS格式时如何在单元格中显示整个数据。我在下面发布java代码。

    ServletContext context = request.getServletContext(); 
    String fullPath = context.getRealPath("/WEB-INF/reports/report5.jrxml");
    InputStream input = new FileInputStream(new File(fullPath));
    JasperDesign jasperDesign = JRXmlLoader.load(input);

    System.out.println("Compiling Report Designs");
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

    System.out.println("Creating JasperPrint Object");
    HashMap<String,Object> map = new HashMap<String,Object>();
    map.put("sql",sql);
    JasperPrint jasperPrint = JasperFillManager.fillReport (jasperReport,map,conn); 
    byte bytes[] = new byte[10000]; 
    //String result = JasperRunManager.runReportToHtmlFile("./usertemplates/test.jasper" , parameters, conn); 



    JRXlsxExporter exporter = new JRXlsxExporter();
    ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); 
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setWhitePageBackground(true);
configuration.setDetectCellType(true);
configuration.setFontSizeFixEnabled(true);
exporter.setConfiguration(configuration);
exporter.exportReport();
    //System.out.println("Size of byte array:"+xlsReport.size()); 
    //bytes = xlsReport.toByteArray();
    String fileName = "InvoiceReport.xls";
response.setHeader("Content-Disposition", "inline; filename="
+ fileName);
    response.setContentType("application/vnd.ms-excel"); 
    //System.out.println("After JasperPrint = 1"); 
    response.setContentLength(xlsReport.size()); 
    //System.out.println("After JasperPrint = 2"); 
    //xlsReport.close(); 
    //System.out.println("After JasperPrint = 3"); 

    OutputStream outputStream = response.getOutputStream(); 
    System.out.println("After JasperPrint = 4"); 
    xlsReport.writeTo(outputStream); 
    outputStream.flush(); 
    //outputStream.close(); 
    } 
    catch(Exception e) 
    {e.printStackTrace();} 

        %>

,jrxml代码如下。

report5.jrxml

<detail>
        <band height="20" splitType="Prevent">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="13" height="20" isPrintWhenDetailOverflows="true" uuid="24b49d3f-5c78-4a19-a4d2-e2cbff95a40d">
                    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
                    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{serial number}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="13" y="0" width="242" height="20" isPrintWhenDetailOverflows="true" uuid="4332ae75-6371-4879-b805-9d07254f0784">
                    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
                    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{DescriptionOfGoods}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="309" y="0" width="86" height="20" isPrintWhenDetailOverflows="true" uuid="54828e8d-bae0-4669-873e-36de8bf54446">
                    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
                    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{PerUnitPrice}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="395" y="0" width="64" height="20" isPrintWhenDetailOverflows="true" uuid="e02408a1-2bcd-4fb9-94d3-ed0c0c1d8ff6">
                    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
                    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
                </textElement>
                <text><![CDATA[Nos]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="459" y="0" width="96" height="20" isPrintWhenDetailOverflows="true" uuid="00ba44d4-b287-4a29-8bf3-1ad07da760e8">
                    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
                    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Quantity} *$F{PerUnitPrice}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="255" y="0" width="54" height="20" isPrintWhenDetailOverflows="true" uuid="d74b9bd4-9608-44a7-b46e-55f39ed0c852">
                    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
                    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Quantity}]]></textFieldExpression>
            </textField>
        </band>
    </detail>

1 个答案:

答案 0 :(得分:1)

首先,您在jasper报告中使用deprecated方法,考虑更新您的代码:

JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
xporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setWhitePageBackground(true);
configuration.setDetectCellType(true);
\\... set all your configuration like above
exporter.setConfiguration(configuration);
exporter.exportReport();

您需要在reportElement的{​​{1}}中设置属性,其中包含需要适合的文本。

textField

如果这不能正常工作,您的解决方案是:

  1. 使excel更改字体大小(适合)

    <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
    <property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
    
  2.   

    标记减小字体大小,使文本符合指定的单元格高度。

    1. 在excel中手动设置列宽
    2. configuration.setFontSizeFixEnabled(true) ,具有以像素为单位的整数值的属性

      如果仍然不满意请转到,请加载工作簿并根据需要进行修改....