使用iReport工具生成报表 PDF

利用iReprot生成报表

首先,下载好iReport工具,打开程序,在这个可以开启一个新的文档。

 开启后得到的一个新档案,右击档案调试模板各部位大小

 通过Band Height调试各部位高度

 

 顶部工具画表格,写文字使用。文字字段多元是我们以后在JAVA程序中传出的数据。

 

 

Parameters 中可以建我们所需要的参数,占位用 $P{ XXX }

 Fields 中建的都是所需要的可循环的数据,占位用$F{XXX}

 建设完毕后点击建立选择所需要生成的模板样式

 编译,运行

 运行后出来的模板效果,以及生成的  .jasper  文件,我们根据生成路径找到它,把它放到需要生成报表的项目中

 

 然后我们就可以使用它生成对应的PDF、XLS的了。下面JAVA代码部分----------

需要的jar包  ---- jasperreports2.0.4.jar  -----itext1.3.1.jar        -----  iTextAsian.jar   --------jasperreports-extensions-1.3.1.jar

 1  首先   编写一个File工具类

 

package com.hnzh.bnk.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.jfinal.log.Log;

import io.jboot.utils.FileUtils;

/**
 * 文件工具类
 * @author zhaoqx
 *
 */
public class FileUtil extends FileUtils{
	
	protected static final Log logger = Log.getLog(FileUtil.class);
	
	public static boolean createFilePath(String directory) {

		File filePath = new File(directory);
		if (!filePath.isDirectory()) {
			return filePath.mkdirs();
		}
		return true;
	}
	
	public static File createFile(String filename, byte[] b) {
		File file = new File(filename);
		try {
			FileOutputStream o = new FileOutputStream(file);
			o.write(b);
			o.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}
	
	/**
	 * 创建单个文件
	 * 
	 * @param descFileName
	 *            文件名,包含路径
	 * @return 如果创建成功,则返回true,否则返回false
	 */
	public static boolean createFile(String descFileName) {
		File file = new File(descFileName);
		if (file.exists()) {
			logger.debug("文件 " + descFileName + " 已存在!");
			return false;
		}
		if (descFileName.endsWith(File.separator)) {
			logger.debug(descFileName + " 为目录,不能创建目录!");
			return false;
		}
		if (!file.getParentFile().exists()) {
			// 如果文件所在的目录不存在,则创建目录
			if (!file.getParentFile().mkdirs()) {
				logger.debug("创建文件所在的目录失败!");
				return false;
			}
		}

		// 创建文件
		try {
			if (file.createNewFile()) {
				logger.debug(descFileName + " 文件创建成功!");
				return true;
			} else {
				logger.debug(descFileName + " 文件创建失败!");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(descFileName + " 文件创建失败!");
			return false;
		}

	}

	public static void copy(File in, File out) throws Exception {
		FileInputStream fis = new FileInputStream(in);
		FileOutputStream fos = new FileOutputStream(out);
		byte[] buf = new byte[1024];
		int i = 0;
		while ((i = fis.read(buf)) != -1) {
			fos.write(buf, 0, i);
		}
		fis.close();
		fos.close();
	}
}

2 ,编写生成报表工具类,对照编写的main方法调用,所有的数据从数据库取出后,存入map中,再将所有数据的map存入list内。map内的key均对应模板中的占位表达式。

package com.hnzh.bnk.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.PathKit;
import com.jfinal.log.Log;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.data.JRMapArrayDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;


/**
 * report报表工具类
 * @author zhaoqx
 *
 */
public class ReportManager {
	
	protected static final Log logger = Log.getLog(ReportManager.class);

	/**
	 * 生成PDF报表
	 * @param dataList
	 * 				封装的报表数据list
	 * @param dataMap
	 * 				报表数据
	 * @param fileName
	 * 				报表名称
	 * @return
	 */
	public static String makeBnkHealthReport(List dataList, Map dataMap,String fileName){
		//C:\Users\Administrator\Desktop
		String filePath = PathKit.getWebRootPath() + File.separator + "report" +File.separator + fileName;
		String strDirPath = PathKit.getWebRootPath() + File.separator + "report-pdf" +File.separator;
		String filename = dataMap.get("PDF_NAME") + ".pdf";    //文件名
		return createReport(dataList, dataMap, strDirPath, filename, filePath);
	}

	public static String createReport(List dataList, Map<String, Object> dataMap, String filePath, String fileName,
			String jasperPath) {
		FileUtil.createFilePath(filePath);
		File file = new File(filePath + File.separator + fileName);
		if (file.exists()) {
			try {
				FileUtil.copy(file,
						new File(filePath + File.separator + fileName + DateUtils.getDate("yyyyMMddHHmmssSSS")));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		byte[] bytes = null;
		try {
			// Object[] objs = new Object[dataList.size()];
			// for (int i = 0; i < dataList.size(); i++) {
			// objs[i] = dataList.get(i);
			// }
			// JRDataSource dataSource = new JRMapArrayDataSource(objs);
			JRDataSource dataSource = new JRBeanCollectionDataSource(dataList);
			bytes = JasperRunManager.runReportToPdf(jasperPath, dataMap, dataSource);
		} catch (JRException e) {
			e.printStackTrace();
		}

		FileUtil.createFile(filePath + File.separator + fileName, bytes);
		System.out.println("create Report success:" + fileName);
		System.out.println(file.getAbsolutePath());
		System.out.println(file.getAbsolutePath().substring(PathKit.getWebRootPath().length(), file.getAbsolutePath().length()));
		return file.getAbsolutePath().substring(PathKit.getWebRootPath().length(),file.getAbsolutePath().length());
		//return file.getAbsolutePath();
	}
	
	public static String createReportXls(List dataList, Map dataMap, String filePath, String fileName,
			String jasperPath) {
		long startTime = System.currentTimeMillis();
		try {
			Object[] objs = new Object[dataList.size()];
			for (int i = 0; i < dataList.size(); i++) {
				objs[i] = dataList.get(i);
			}
			JRDataSource dataSource = new JRMapArrayDataSource(objs);
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, dataMap, dataSource);
			JRXlsExporter exporter = new JRXlsExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, (filePath + File.separator + fileName));
			exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
			exporter.exportReport();
			long endTime = System.currentTimeMillis();
			System.out.println(System.currentTimeMillis());
			long time = (endTime - startTime) / 1000;
			System.out.println("success with " + time + " s");
			return (filePath + File.separator + fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		Map dataMap = new HashMap<String, Object>();
		dataMap.put("PDF_NAME", "测试数据");   //文件名
		dataMap.put("CONSULTATION_NAME", "123123");   
		Map map = new HashMap<String, Object>();
		/*map.put("HOSPITAL_NAME", "hello world");*/
		// 项目下一张图片的路径
		/*String imagePath = PathKit.getWebRootPath() + "upload" + File.separator + "e40229b62529455a9248d2c0c30d1be4.png";
		map.put("picture_address", imagePath);// 流
		dataList.add(map);
		map = new HashMap<String, Object>();
		imagePath = PathKit.getWebRootPath() + "upload" + File.separator + "fbe2b0b26107449abc78b8065f7c43db.png";
		map.put("picture_address", imagePath);// 流
*/		dataList.add(map);
        //生成的PDF路径
		String makeBnkHealthReport = makeBnkHealthReport(dataList, dataMap,"platformMedicalRecords.jasper");
		//makePDF(dataList, dataMap, 1);
	}
}

(以PDF为例) 程序执行后,返回生成的PDF路径,然后我们将该URL传入前端,以PDF格式显示

<object data="#(platformMedicalRecordsPDF)" type="application/pdf" width=1190px" height="790px">
 
		<iframe src="#(platformMedicalRecordsPDF)" width="1190px" height="790px" style="border: none;">
		 
		This browser does not support PDFs. Please download the PDF to view it: <a href="#(platformMedicalRecordsPDF)">Download PDF</a>
		 
		</iframe>
		 
		</object>

main方法测试生成效果:

 项目内运行效果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值