我试图在Scala中编写一个Jasper Report程序,其中数据结构是动态创建的。数据源是Data
个对象的列表(参见下面的case类),其中每个Data对象都是字符串和Ints的列表。
报告中的每一行都有Data.strings(0),Data.strings(1)等字段。
import scala.collection.JavaConversions._
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import net.sf.jasperreports.engine._
import net.sf.jasperreports.engine.data._
case class Data (strings: Array[String], ints: Array[Int] )
val str1 = Array("String11", "String12")
val int1 = Array(11,12)
val str2 = Array("String21", "String22")
val int2 = Array(21,22)
val data1 = Data(str1, int1)
val data2 = Data(str2, int2)
val dataSource = new JRBeanArrayDataSource(Array(data1, data2));
val params = new HashMap[String,String](Map("1"->"one", "2"->"two"))
val jasperPrint = JasperFillManager.fillReport("test1.jasper", params, dataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "test1.pdf");
问题是JasperFillManager.fillReport
行没有使用以下错误进行编译,显然是因为不支持Data对象列表:
◾重载方法值fillReport与备选方案:(x $ 1: net.sf.jasperreports.engine.JasperReport,X $ 2: java.util.Map [字符串,对象],X $ 3: net.sf.jasperreports.engine.JRDataSource)net.sf.jasperreports.engine.JasperPrint (x $ 1:java.io.InputStream,x $ 2:java.util.Map [String,Object],x $ 3: net.sf.jasperreports.engine.JRDataSource)net.sf.jasperreports.engine.JasperPrint (x $ 1:String,x $ 2:java.util.Map [String,Object],x $ 3: net.sf.jasperreports.engine.JRDataSource)net.sf.jasperreports.engine.JasperPrint (x $ 1:net.sf.jasperreports.engine.JasperReport,x $ 2: java.util.Map [字符串,对象],X $ 3: java.sql.Connection中)net.sf.jasperreports.engine.JasperPrint (x $ 1:java.io.InputStream,x $ 2:java.util.Map [String,Object],x $ 3: java.sql.Connection中)net.sf.jasperreports.engine.JasperPrint (x $ 1:String,x $ 2:java.util.Map [String,Object],x $ 3: java.sql.Connection)net.sf.jasperreports.engine.JasperPrint不能 apply to(String,java.util.HashMap [Int,String], net.sf.jasperreports.engine.data.JRBeanArrayDataSource)
有关如何实现这一目标的任何想法?
答案 0 :(得分:0)
这解决了这个问题:明确地将HashMap声明为[String,Object]:
val params = new HashMap[String,Object](Map("1"->"one", "2"->"two"))