我的情况与案件非常相似: Jasper Report with Struts2 Framework
一切正常并生成PDF文件。代码类似,配置 struts.xml 类似,或者我们可以说它是相同的。
我尝试以 RTF格式生成报告。这很简单,正确?只需替换 struts.xml 中的一行 - &gt; <param name="format">RTF</param>
。
格式 - 应生成报告的格式。有效值可以在JasperReportConstants中找到。
在JasperReportConstants下有“FORMAT_RTF”。这意味着值为“RTF”。没有其他的。或者......我错过了什么吗?
系统会生成 pdf-document ,其扩展名为“ rtf ”,而不是真正的 RTF格式文档。
有什么问题?我错过了什么?
答案 0 :(得分:0)
我希望您使用正确且兼容的 itext 和 jasper Repot。
<param name="format">RTF</param>
是RTF的唯一更改。
这是一个有效的例子
<package name="default" namespace="/" extends="jasperreports-default">
<action name="myJasperTest" class="com.common.JasperAction">
<result name="success" type="jasper">
<param name="location">/jasper/our_compiled_template.jasper</param>
<param name="dataSource">myList</param>
<param name="format">RTF</param>
</result>
</action>
</package>
如果您使用与Jasper Report with Struts2 Framework相同的代码,则需要更改代码。因为在该代码中,它们以pdf格式导出。
您可以使用
try {
JasperCompileManager.compileReportToFile(
"C:\\Users\\Parth\\workspace1\\serializ\\jasper/our_jasper_template.jrxml",
"H:\\jasper/our_compiled_template.jasper");
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
JasperAction.java
package com.common;
import java.util.ArrayList;
import java.util.List;
import net.sf.jasperreports.engine.JasperCompileManager;
import com.Person;
import com.opensymphony.xwork2.ActionSupport;
public class JasperAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private List<Person> myList;
public String execute() throws Exception {
// Create some imaginary persons.
Person p1 = new Person(new Long(1), "Patrick", "Lightbuddie");
Person p2 = new Person(new Long(2), "Jason", "Carrora");
Person p3 = new Person(new Long(3), "Alexandru", "Papesco");
Person p4 = new Person(new Long(4), "Jay", "Boss");
// Store people in our dataSource list (normally they would come from a database).
myList = new ArrayList<Person>();
myList.add(p1);
myList.add(p2);
myList.add(p3);
myList.add(p4);
try {
JasperCompileManager.compileReportToFile(
"C:\\Users\\Parth\\workspace1\\serializ\\jasper/our_jasper_template.jrxml",
"H:\\jasper/our_compiled_template.jasper");
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
public List<Person> getMyList() {
return myList;
}
}
<强> Person.java 强>
package com;
public class Person {
private Long id;
private String name;
private String lastName;
public Person() {
}
public Person(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
public Person(Long id, String name, String lastName) {
this.id = id;
this.name = name;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
our_jasper_template.jrxml
<?xml version="1.0"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="jasper_test">
<!-- Our fields from the Person class. -->
<field name="name" class="java.lang.String"/>
<field name="lastName" class="java.lang.String"/>
<title>
<band height="50">
<staticText>
<reportElement x="0" y="0" width="180" height="15"/>
<textElement/>
<text><![CDATA[Struts 2 JasperReports Sample]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band/>
</pageHeader>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[NAME]]></text>
</staticText>
<staticText>
<reportElement x="360" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[LASTNAME]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20">
<textField>
<reportElement x="180" y="0" width="180" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="360" y="0" width="180" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band/>
</columnFooter>