JAVA预览表格_Java预览JasperReport报表

本文章将向读者展示怎样通过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 parameter = new HashMap();

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();

}

}

执行效果图如下:

095222d591f15e9ded0e03031f044d16.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值