我正在尝试测试jasperreport并编写Java代码,但它给了我错误:
-------------------- PDF exception
net.sf.jasperreports.engine.JRException:
Error loading byte data : C:\Users\mt\Desktop\project20111116\test
\src3\report2_1336431810533_776931.class
我做错了什么?
代码:
public static void main(String[] args) {
try {
File jrxmlFile = new File("C:\\Users\\mt\\report2.jrxml");
if(jrxmlFile.exists()) {
//jrxml complie
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile.getAbsolutePath());
List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
for (int i = 0; i < 10; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("orderNumber", "hello");
map.put("anotherNumber", "world");
//map.put("canIUseArray", new String[]{"foo", "bar", "moo", "pee"});
list.add(map);
}
JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(list);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), dataSource);
JasperViewer.viewReport(jasperPrint, false);
}
}
catch(Exception e) {
System.out.println("-------------------- PDF exception ");
System.out.println(e);
}
System.out.println("DONE");
}//end main
答案 0 :(得分:2)
这是因为jrxml没有指定使用的语言。
在我的情况下,我将其设置为“groovy”,代码工作正常。
(在jasperReport标签中,添加了 language =“groovy”属性)
<?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="report2"
language="groovy"
pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.2100000000000006"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="table">