IReport在设置参数的时候需要先在左边新建一个parameter,
然后再把该parameter托到右边的设计栏中。
ireport只是一个设计工具,把它生成的.jasper文件放到项目中,然后在Action或servlet中进行处理。代码大概如下:
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource()FileInputStream fis = new FileInputStream(new File("xxx.jasper"))
Map<String,Object> parameters = new HashMap<String,Object>()
JasperPrint jasperPrint = JasperFillManager.fillReport(fis, parameters, dataSource)
byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint)
//得到bytes就可以写入到文件中了
不过说实话ireport的学习要求比较高,一般人很难上手,如果是初学者的话不是很介意使用,可以用一些比较简单的例如finereport等报表软件进行进阶。
将需要批量打印的内容生成的结果集传递给报表模板就可以了,会自动批量打印。不预览循环批量打印要怎样实现?
需要调用打印接口 JasperPrintManager类可以直接实现打印。具体资料你可以参考Api
我用的是printReport 方法。
主要就是先在服务器端生成JasperPrint对象,返回给客户端。
下面代码百度到的,你可以参考。
import java.io.File
import java.io.ObjectOutputStream
import java.util.ArrayList
import java.util.List
import java.util.Map
import javax.naming.Context
import javax.naming.InitialContext
import javax.naming.NameNotFoundException
import javax.servlet.ServletOutputStream
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.sql.DataSource
import net.sf.jasperreports.engine.JRException
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperPrint
import net.sf.jasperreports.engine.JasperPrintManager
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import org.apache.struts.action.Action
import org.apache.struts.action.ActionForm
import org.apache.struts.action.ActionForward
import org.apache.struts.action.ActionMapping
import com.lowagie.text.ExceptionConverter
import com.sofn.struts.dto.UserInfoDTO
import com.sofn.struts.op.UserInfo
import com.sofn.util.UserSessionOperate
public class ReportViewAction extends Action {
(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws Exception {
//已编译文件路径和格式
String fp =this.getServlet().getServletContext().getRealPath("/")+ "report\\jasper\\"+ request.getAttribute("rdoc")+ ".jasper"
File reportFile = new File(fp)
//System.out.println("报表路径!!!!!!!"+reportFile)
Map parameters = (Map) request.getAttribute("paras")
byte[] bytes = null
List list = new ArrayList()
DataSource ds = null
JasperPrint jasperPrint=null
//参数传递
UserSessionOperate uso = new UserSessionOperate()
String user = uso.getUser(request)
UserInfo eidr = new UserInfo()
UserInfoDTO eidrdto = null
String deptname = "物流部门"
String username = "陈云刚"
parameters.put("marker", username)
parameters.put("bumen", deptname)
//数据库连接
try {
String source = "java:comp/env/jdbc/posuse"
Context ic = null
if (ic == null)
ic = new InitialContext()
ds = (DataSource) ic.lookup(source)
if (ds == null) {
System.out.println("Reportview----22222!")
}
} catch (NameNotFoundException nfe) {
nfe.toString())
} catch (Exception ne) {
ne.printStackTrace()
}
//使用数据源
if (request.getAttribute("datasource") != null) {
list = (List) request.getAttribute("reportlist")
JRBeanCollectionDataSource jds =new JRBeanCollectionDataSource(list) //使用数据源
}
//使用SQL查询
try {
jasperPrint =JasperFillManager.fillReport(reportFile.getPath(),parameters,ds.getConnection())//填充报表数据生成JasperPrint对象
JasperPrintManager.printReport(jasperPrint, false)//2008-08-25 直接打印,不用预览PDF直接打印 true为d出打印机选择.false为直接打印.
} catch (JRException jre) {
//System.out.println("Reportview----JRException88888")
jre.printStackTrace()
} catch (ExceptionConverter ec) {
ec.getLocalizedMessage())
ec.printStackTrace()
} catch (NullPointerException npe) {
npe.getLocalizedMessage())
npe.getMessage())
npe.toString())
System.err)
} catch (Exception ee) {
ee.printStackTrace()
}
//输出
response.setContentType("application/octet-stream")
ServletOutputStream ouputStream = response.getOutputStream()
ObjectOutputStream oos = new ObjectOutputStream(ouputStream)
oos.writeObject(jasperPrint)//将JasperPrint对象写入对象输出流中
oos.flush()
oos.close()
ouputStream.flush()
ouputStream.close()
return mapping.findForward("view") //转向
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)