gpt4 book ai didi

java - 从 .jasper 文件获取 JasperPrint 对象

转载 作者:行者123 更新时间:2023-12-01 23:42:27 27 4
gpt4 key购买 nike

我正在尝试获取 JRPrint 对象,因此代码如下:

import java.io.File;

import net.sf.jasperreports.engine.*;

public class TestClass {
protected static JasperPrint jasperPrint;
static JasperReport jasperReport;
protected static JasperReportsContext jasperReportsContext;

public static void main(String[] args) {
String fileName = "/home/amira/Desktop/Map/testReports/test/textreport.jasper";
boolean isXMLFile = false;

if (!isXMLFile && fileName.endsWith(".jrxml"))
{
isXMLFile = true;
}

try
{
loadReportJrprint(fileName, isXMLFile, DefaultJasperReportsContext.getInstance());
}
catch (JRException e)
{

System.err.println("Error viewing report design."+ e);

System.exit(1);
}

try {
JasperExportManager.exportReportToPdfFile(fileName);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


protected static void loadReportJrprint(String fileName, boolean isXmlReport, JasperReportsContext jasperReportsContext) throws JRException
{
if (isXmlReport)
{
jasperPrint = JRPrintXmlLoader.loadFromFile(jasperReportsContext, fileName);
System.out.println(jasperPrint.getName());

}
else
{
jasperPrint = (JasperPrint)JRLoader.loadObjectFromFile(fileName);
}

}
}

但我收到此错误:

Exception in thread "main" java.lang.ClassCastException: net.sf.jasperreports.engine.JasperReport cannot be cast to net.sf.jasperreports.engine.JasperPrint
at TestClass.loadReportJrprint(TestClass.java:80)
at TestClass.main(TestClass.java:50)

最佳答案

.jasper 文件仅仅是一个已编译的 .jrxml,因此您检索的是一个 JasperReport 对象,它描述了已编译的模板。要获取描述可供查看或导出的文档的 JasperPrint 对象,您必须首先使用 JasperFillManager.fill 方法之一填充报表。请参阅the javadoc for JasperFillManager .

关于java - 从 .jasper 文件获取 JasperPrint 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17695845/

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