gpt4 book ai didi

java - Jasper Report用Struts2框架生成RTF格式

转载 作者:行者123 更新时间:2023-11-29 08:57:08 24 4
gpt4 key购买 nike

我的案例与案例非常相似: Jasper Report with Struts2 Framework

一切正常并生成 PDF 文件。代码相似,配置 struts.xml 也相似,或者说是一样。

我尝试生成RTF 格式 的报告。这很简单,对吧?只需替换struts.xml中的一行--> <param name="format">RTF</param> .

API 表示:

Format - the format in which the report should be generated. Valid values can be found in JasperReportConstants.

在 JasperReportConstants 下有“FORMAT_RTF”。这意味着该值为“RTF”。没有其他的。或者...我错过了什么吗?

系统生成带有“rtf”扩展名的pdf文档,而不是真正的RTF格式文档

怎么了?我错过了什么?

最佳答案

我希望您使用的是 itextjasper Repot 的正确且兼容的 jar。 <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>

关于java - Jasper Report用Struts2框架生成RTF格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19659366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com