ireport报表怎样实现批量打印,请高手帮忙。
将需要批量打印的内容生成的结果集传递给报表模板就可以了,会自动批量打印。
不预览循环批量打印要怎样实现?
需要调用打印接口 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为弹出打印机选择.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"); //转向
}
}
怎样实现JasperReport循环打印 or 多表打印
jasperreport的话不是很清楚,一般情况下都是需要代码的,我现在常用的报表软件是finereport,比较简单,可以实现批量打印。如需要打印出某个模板所有参数情况对应的结果,如下图,选择希望打印的参数,点击doprint按钮批量打印出对应的结果。首先通过JS获取复选框的值然后拼凑出正确的URL,最后调用打印方法如PDF打印,通过post传参或get传参进行批量打印。
jasperreports一次打印多张报表怎么实现
1、若是采用bean作为数据源,可将多个条数据组装成List:
List list = new ArrayList();
list.add(xxx);
list.add(xxx);
......
2、将List装入中:
JRDataSource source =new JRBeanCollectionDataSource(list)
3、初始化jasperReport 对象:
JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile("你的.jasper文件路径");
4、构建JasperPrint 对象,将jasperReport 对象,source数据源填充到report中:
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null, source);
5、获取打印机列表:
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.PNG;
PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, pras);
6、指定打印机打印:
for (PrintService p : printService) {
if("指定的打印机名称".equals(p.getName())){
JRAbstractExporter je=new JRPrintServiceExporter();
//传入jasperPrint对象
je.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
je.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE,p);
je.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,false);
je.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,false);
je.exportReport();
}
}