本文章将向读者展示怎样通过Java代码去为我们设计好的jasper报表填充数据(使用json作为数据源)和预览报表。关于报表的制作请查看《第一个JasperReport项目HelloWorld》文章。下面是实现java代码:
package com.huangx; import java.io.ByteArrayInputStream; import java.util.HashMap; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.HashPrintServiceAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.PrintServiceAttributeSet; import javax.print.attribute.standard.MediaSizeName; import javax.print.attribute.standard.PrinterName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import com.alibaba.fastjson.JSONObject; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JsonDataSource; import net.sf.jasperreports.engine.export.JRPrintServiceExporter; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.export.SimpleExporterInput; import net.sf.jasperreports.export.SimplePrintServiceExporterConfiguration; import net.sf.jasperreports.view.JasperViewer; /** * 打印和预览报表 * @author Administrator * @date 2017年8月8日 16:10:20 */ public class JasperHello { private static final Logger LOG = LoggerFactory.getLogger(JasperHello.class); public static void main(String[] args) { new JasperHello(); } public JasperHello() { try { JasperReport jasperReport = (JasperReport)JRLoader.loadObject( new ClassPathResource("hello.jasper").getInputStream()); // 填充报表数据 JSONObject data = new JSONObject(); data.put("title", "Hello World"); data.put("content", "您好!这是一个测试报表,如果你能看见这条信息,则说明测试Jaspersoft成功了。恭喜!!"); data.put("date", "2017年8月14日"); JsonDataSource jsonDataSource = new JsonDataSource( new ByteArrayInputStream(data.toJSONString().getBytes())); HashMap<String, Object> parameter = new HashMap<String, Object>(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameter, jsonDataSource); // 打印报表 // print(jasperPrint); // 预览报表 preview(jasperPrint); } catch(Exception e) { LOG.error(e.getMessage(), e); } } /** * 预览报表 * @param jasperPrint */ protected void preview(JasperPrint jasperPrint) { JasperViewer.viewReport(jasperPrint, false); } /** * 打印报表 * @param jasperPrint * @throws JRException */ protected void print(JasperPrint jasperPrint) throws JRException { JRPrintServiceExporter exporter = new JRPrintServiceExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); SimplePrintServiceExporterConfiguration configuration = new SimplePrintServiceExporterConfiguration(); // 设置打印机请求参数 PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.ISO_A4); configuration.setPrintRequestAttributeSet(printRequestAttributeSet); // 打印机服务参数 PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(); // 获取默认打印服务 PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); if ( null == printService ) { throw new NullPointerException("没有找到打印服务,请设置本机默认的打印机服务"); } LOG.debug("{}", printService); printServiceAttributeSet.add(new PrinterName(printService.getName(), null)); configuration.setPrintServiceAttributeSet(printServiceAttributeSet); // 设置是否显示页面设置页面 configuration.setDisplayPageDialog(false); // 设置是否显示打印机选择界面 configuration.setDisplayPrintDialog(true); exporter.setConfiguration(configuration); // 执行打印 exporter.exportReport(); } }
执行效果图如下: