2016-01-28 49 views
1

我有一個帶有靜態字段和文本字段的ireport。數據庫由大部分數據組成,對於數據庫中的單個行有多行數據。因此,當我我正在通過java代碼將jasper報告導出爲PDF格式,它正在擴展和詳細描述和打印整個數據,而不會丟失任何行,但是當我輸出到XLS時,它缺少數據,而且單元格的伸展不會發生。顯示圖像以便更好地理解。 enter image description here將jasper報告導出爲XLS格式時沒有發生單元格擴展

正如你可以在上面看到的數據打印非常清晰的PDF格式。 enter image description here

在上面,這是XLS格式數據隱藏或在感覺missing.So我的問題是如何顯示報告導出到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> 
+0

我把這兩個在但它不工作。

回答

1

首先你正在使用碧玉報告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(); 

您需要在textFieldreportElement的設置屬性包含需要適合的文本。

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

如果這是不工作的期望您的解決方案是:

  1. 使Excel改變字體大小(以適應)

    configuration.setFontSizeFixEnabled(true) 
    

標誌用於減少字體大小,以便文本符合指定的單元格高度。

  • 手動設置列的寬度在Excel
  • net.sf.jasperreports.export.xls.column.width,與以像素爲單位

    如果一個整數值屬性您仍不滿意轉 ,加載工作簿並修改它,只要你喜歡....

    +0

    我已經完成了你所說的,但它仍然是一樣的。不擴展cell.You可以看到上面的代碼被編輯 –

    +0

    在excel中發生了什麼?任何更改? –

    +0

    否。在PDF格式中,它正在擴展,如上所示,並且以XLS格式顯示一些正在打印並且一些正在隱藏 –

    相關問題