2013-03-06 52 views
0

我使用jasper來顯示我的項目報告......其實我試圖將參數動態地傳遞給jrxml模板,正如我在下面給出的鏈接中看到的但我不能.....似乎在我的代碼中的錯誤是net.sf.jasperreports.engine.JRException:從文件加載對象時出錯

 net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Users\db2admin\Documents\NetBeansProjects\ConnectDB\src\sample_report.jrxml 
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:115) 
    at net.sf.jasperreports.engine.JasperFillManager.fillToFile(JasperFillManager.java:487) 
    at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:874) 
    at ConnectDB.main(ConnectDB.java:65) 
Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D 
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801) 
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298) 
    at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:53) 
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:110) 
    ... 3 more 

碧玉片段是

ResultSet resultSet = statement.executeQuery("SELECT reg, name FROM students"); 
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(resultSet);   

JasperFillManager.fillReportToFile("C:\\Users\\db2admin\\Documents\\NetBeansProjects\\ 
      ConnectDB\\src\\sample_report.jrxml", null, resultSetDataSource); 

最後的XML代碼

<?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="sample_report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="b3b98795-b21f-4006-9295-5ef4248c84ea"> 
    <property name="ireport.zoom" value="1.0"/> 
    <property name="ireport.x" value="0"/> 
    <property name="ireport.y" value="0"/> 

    <field name="name" class="java.lang.String"/> 
    <field name="reg" class="java.lang.Integer"/> 
    <columnHeader> 
     <band height="28" splitType="Stretch"> 
      <staticText> 
       <reportElement uuid="6d6bff96-6dcb-4188-bdc0-5c23b2614b05" key="staticText-1" x="40" y="11" width="196" height="15"/> 
       <textElement/> 
       <text><![CDATA[Name]]> 
       </text> 
      </staticText> 
      <staticText> 
       <reportElement uuid="c0267b29-e31b-4afd-a181-86bbcb4fb368" key="staticText-2" x="330" y="11" width="193" height="15"/> 
       <textElement/> 
       <text><![CDATA[Reg]]> 
       </text> 
      </staticText> 
     </band> 
    </columnHeader> 
    <detail> 
     <band height="27" splitType="Stretch"> 
      <textField> 
       <reportElement uuid="4a1e6484-9610-421d-b4a4-f61d53458d9e" key="textField" x="47" y="6" width="173" height="18"/> 
       <textElement/> 
       <textFieldExpression><![CDATA[$F{name}]]> 
       </textFieldExpression> 
      </textField> 
      <textField> 
       <reportElement uuid="01cb3d91-74e7-4594-aca0-605d1aa203a8" key="textField" x="330" y="6" width="100" height="18"/> 
       <textElement/> 
       <textFieldExpression><![CDATA[$F{reg}]]> 
       </textFieldExpression> 
      </textField> 
     </band> 
    </detail> 
</jasperReport> 
問題

我提到這個網站 http://www.packtpub.com/article/creating-dynamic-reports-from-databases-jasperreports-3.5

PLZ幫助我!!!!

+0

您使用的是什麼版本的* JR API * – 2013-03-06 15:46:52

+0

該版本是4.7.1 bro! – 2013-03-06 16:45:21

+1

啊,我知道了... JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(resultSet); JasperReport jasperReport = JasperCompileManager.compileReport(「C:\\ Users \\ db2admin \\ Documents \\ NetBeansProjects \\ ConnectDB \\ src \\ sample_report.jrxml」); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,resultSetDataSource); JasperViewer.viewReport(jasperPrint); – 2013-03-06 17:11:25

回答

1

如果您在ireport工作,請檢查您的子報表是否引用了.jasper文件(而不是jrxml)。

如果您要將報告發布到jasperreports服務器,請記住,它會在引用具有前綴(但不包含jasper文件)的jrxml文件的報告定義中進行更改。

+0

這是我的問題。非常煩人的處理服務器和本地之間的這些表達式變化,真希望@JasperSoft能夠修復。 – AlexVPerl 2014-03-18 18:14:28