jasperReport+struts2打印

所需jar包,如下:
1、groovy-all-1.7.5.jar
2、iText-2.1.7.jar [生产PDF文档使用]
3、iTextAsian.jar [语言支持]
4、jasperreports4.6.0.jar [必备包]
5、poi-3.7.jar[导出excel时候用]
6、commons-collections-3.2.jar
7、commons-beanutils-1.8.2.jar
8、commons-digester-2.1.jar

9、org-netbeans-core.jar [可有可无(style字体颜色)]

下面是我自己写的工具类

package com.util;

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

import javax.servlet.ServletContext;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class ReportUtils {

	protected static final String  DPF_PATH = "/download/";
	/**
	 * 生产 .jasper 文件 并生成PDF文件
	 * @param sourceFileName
	 * 			.jrxml文件的位置
	 * @param destFileName
	 * 			生成 .jasper 文件的名称
	 * @param list
	 * 			需要循环打印的集合
	 * @param map
	 * 			打印一次的参数(标题,表头、表尾的数据)
	 * @param title
	 * 			PDF文件的名称
	 * @param code
	 * 			打印的单号 (title + code = PDF文件名)
	 * @return	返回PDF文件所在的路径
	 * @throws Exception
	 */
	public static String runReportToPdfFile(String sourceFileName,String  destFileName,List list,
			Map<String, Object> map,String title,String code) throws Exception{

		ServletContext context = ServletActionContext.getServletContext();
		//获取.jrxml 在磁盘的根路径
		String reportSource = context.getRealPath(sourceFileName);
		//获取.jrxml 在磁盘的上级目录
		File parent = new File(reportSource).getParentFile();
		//获取.jasper 在磁盘的路径
		String destFilePath = new File(parent, destFileName).getAbsolutePath();
		// 将.jrxml模板文件编译成为.jasper文件,当然,其文件名可以指定,如果没指定,则与.jrxml文件名一样.只是后缀不同而已
		JasperCompileManager.compileReportToFile(reportSource, destFilePath);
		
		
		//转换list集合
		JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);
		//生成PDF文件和web PDF文件并返回PDF文件路径
		String pdfPaht = JasperRunManager.runReportToPdfFile(destFilePath, map, jrDataSource);
		//旧的PDF文件
		File oldpath = new File(pdfPaht);
		//获取磁盘下 项目的 DPF_PATH 路径
		String dpfPath = context.getRealPath(DPF_PATH)+ File.separator;
		//获取项目的  DPF_PATH 路径
		String realPath = context.getContextPath()+ DPF_PATH;
		File dir = new File(realPath);
		if (!dir.exists()) {// 没有目录就创建目录
			dir.mkdirs();
		}
		//PDF文件名称
		String name = title + "_" + code +".pdf";
		//新PDF文件名称及路径
		File savePDFFile = new File(dpfPath, name);
		//把旧PDF文件copy到新文件路径
		FileUtils.copyFile(oldpath, savePDFFile);
		//删除旧PDF文件
		oldpath.delete();
		
		return realPath + name;
		
		}
	/**
	 * 生产 .jasper 文件 并生成PDF文件
	 * @param sourceFileName
	 * 			.jrxml文件的位置
	 * @param list
	 * 			需要循环打印的集合
	 * @param map
	 * 			打印一次的参数(标题,表头、表尾的数据)
	 * @param title
	 * 			PDF文件的名称
	 * @return 返回PDF文件所在的路径
	 * @throws Exception
	 */
	public static String runReportToPdfFile(String sourceFileName,List list,
							Map<String, Object> map,String title) throws Exception{

		ServletContext context = ServletActionContext.getServletContext();
		//获取.jrxml 在磁盘的根路径
		String reportSource = context.getRealPath(sourceFileName);
		//获取.jrxml 在磁盘的上级目录
		File parent = new File(reportSource).getParentFile();
		//获取.jasper 在磁盘的路径
		String[] destFileNames = sourceFileName.split("/");
		String destFileName = destFileNames[destFileNames.length-1];
		destFileName = destFileName.substring(0,destFileName.length()-6) ;
		String destFilePath = new File(parent, destFileName + ".jasper").getAbsolutePath();
		// 将.jrxml模板文件编译成为.jasper文件,当然,其文件名可以指定,如果没指定,则与.jrxml文件名一样.只是后缀不同而已
		JasperCompileManager.compileReportToFile(reportSource, destFilePath);
		
		
		//转换list集合
		JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);
		//生成PDF文件和web PDF文件并返回PDF文件路径
		String pdfPaht = JasperRunManager.runReportToPdfFile(destFilePath, map, jrDataSource);
		//旧的PDF文件
		File oldpath = new File(pdfPaht);
		//获取磁盘下 项目的 DPF_PATH 路径
		String dpfPath = context.getRealPath(DPF_PATH)+ File.separator;
		//获取项目的  DPF_PATH 路径
		String realPath = context.getContextPath()+ DPF_PATH;
		File dir = new File(realPath);
		if (!dir.exists()) {// 没有目录就创建目录
			dir.mkdirs();
		}
		//PDF文件名称
		if(title.equals("null") || title == null || title == ""){
			title = destFileName;
		}
		String name = title + ".pdf";
		//新PDF文件名称及路径
		File savePDFFile = new File(dpfPath, name);
		//把旧PDF文件copy到新文件路径
		FileUtils.copyFile(oldpath, savePDFFile);
		//删除旧PDF文件
		oldpath.delete();
		
		return realPath + name;
		
		}
	/**
	 * 生成 .jasper 文件
	 * @param sourceFileName
	 * 			.jrxml文件的位置
	 * @throws Exception
	 */
	public static void compileReportToFile(String sourceFileName) throws Exception{
		String reportSource = ServletActionContext.getServletContext().getRealPath(sourceFileName);
		File parent = new File(reportSource).getParentFile();
		String[] destFileNames = sourceFileName.split("/");
		String destFileName = destFileNames[destFileNames.length-1];
		destFileName = destFileName.substring(0,destFileName.length()-6) + ".jasper";
		// 将.jrxml模板文件编译成为.jasper文件,当然,其文件名可以指定,如果没指定,则与.jrxml文件名一样.只是后缀不同而已                                 
		JasperCompileManager.compileReportToFile(reportSource, new File(parent, destFileName).getAbsolutePath());
	}
}

这里是调用类

package com.action.purchase.report;

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

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.entity.PaymentOrder;
import com.util.ReportUtils;
import com.util.action.ModelDrivenBaseAction;


@Controller
@ParentPackage(value = "jasperreports-default")
@Namespace(value = "/purchase") 
public class ReportTest extends ModelDrivenBaseAction<PaymentOrder> {
	protected Map<String, Object> map = new HashMap<String, Object>();
	protected List list = new ArrayList();
	@Action(value = "textReport", results = { @Result(type = "jasper", params = {
			"location", "/report/test.jasper", "format",
			"PDF", "dataSource", "list", "reportParameters", "map" }) })
	public  String text() throws Exception {
		//Map<String, Object> map = new HashMap<String, Object>();
		//List list = new ArrayList();
		map.put("title", "xxx报表");
		map.put("name", "zhangsan");
		map.put("age", "20");
		map.put("sex", "男");
		map.put("height", "170cm");
		map.put("phone", "13800000000");
		map.put("email", "13800000000@163.com");
		Map<String, Object> maps;
		for (int i = 1; i <= 50; i++) {
			maps = new HashMap<String, Object>();
			maps.put("address", "地址" + i);
			maps.put("shopping", "物品" + i);
			maps.put("money", "金额" + i);
			maps.put("piece", "件数" + i);
			maps.put("weight", "重量" + i);
			maps.put("share", "份数" + i);
			list.add(maps);
		}
		//这里调用
		ReportUtils.compileReportToFile("/report/test.jrxml");
		return SUCCESS;
	}
	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	
}

jrxml和成功打印的PDF文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值