导入这个包jasperreports-2.0.3.jar
安装报表软件
package com.dao;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set;import javax.naming.Context;
import javax.naming.InitialContext; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource;import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.JasperRunManager; import net.sf.jasperreports.engine.export.JRRtfExporter; import net.sf.jasperreports.engine.util.JRLoader;import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class ReportHandle {
/**
* 打印报表 * * reportPath * 报表路径:"/report/demo.jasper" * ParamName * 报表参数名称:默认 "SQLSTR",建议不修改 * SqlStr * SQL参数内容:"select * from tdc001" * reportType * 报表类型:ReportType.PDF , ReportType.HTML... * varMaps * 其他参数 * @throws Exception */ public static void PrintReport(String reportPath, String ParamName, String SqlStr, ReportType reportType, Map varMaps) throws Exception {ReportData(reportPath, ParamName, SqlStr, reportType, varMaps);
}/**
* 结合数据库产生报表 * * @param reportPath * 报表路径:"/report/demo.jasper" * @param ParamName * 报表参数名称:"SQLSTR" * @param SqlStr * SQL参数内容:"select * from tdc001" * @param reportType * 报表类型:ReportType.PDF , ReportType.HTML... * @param varMaps * 其他参数 * @throws Exception */ @SuppressWarnings("unused") private static void ReportData(String reportPath, String ParamName, String SqlStr, ReportType reportType, Map varMaps) throws Exception { Connection con = null; // 初始化查找命名空间 Context initContext = new InitialContext(); // 找到DataSource DataSource ds = (DataSource) initContext .lookup("java:/comp/env/jdbc/oracleDb"); try { con = ds.getConnection(); HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = (HttpServletRequest) ActionContext .getContext().get( org.apache.struts2.StrutsStatics.HTTP_REQUEST);Map parameters = new HashMap();
File reportFile = new File(request.getRealPath(reportPath)); // SQL参数 parameters.put(ParamName, SqlStr);// 其他参数
if (null != varMaps && varMaps.size() > 0) { Set set = varMaps.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); parameters.put((String) mapentry.getKey(), (String) mapentry.getValue()); } } byte[] bytes = JasperRunManager.runReportToPdf( reportFile.getPath(), parameters, con); response.setContentType("application/pdf"); response.setContentLength(bytes.length); ServletOutputStream outStream = response.getOutputStream(); outStream.write(bytes, 0, bytes.length);// /写入对象输出流中 outStream.flush(); outStream.close();} catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != con) { try { con.close(); } catch (SQLException e) { con = null; e.printStackTrace(); } } } }/**
* 结合数据库产生报表 * * @param reportPath * 报表路径:"/report/demo.jasper" * @param ParamName * 报表参数名称:"SQLSTR" * @param SqlStr * SQL参数内容:"select * from tdc001" * @param reportType * 报表类型:ReportType.PDF , ReportType.HTML... * @param varMaps * 其他参数 * @throws Exception */ @SuppressWarnings("unused") public static void ExportReport(String reportPath, String ParamName, String SqlStr, ReportType reportType, Map varMaps) throws Exception { Connection con = null; // 初始化查找命名空间 Context initContext = new InitialContext(); // 找到DataSource DataSource ds = (DataSource) initContext .lookup("java:/comp/env/jdbc/oracleDb"); try { con = ds.getConnection(); HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = (HttpServletRequest) ActionContext .getContext().get( org.apache.struts2.StrutsStatics.HTTP_REQUEST);Map parameters = new HashMap();
File reportFile = new File(request.getRealPath(reportPath)); // SQL参数 parameters.put(ParamName, SqlStr);// 其他参数
if (null != varMaps && varMaps.size() > 0) { Set set = varMaps.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); parameters.put((String) mapentry.getKey(), (String) mapentry.getValue()); } } if (reportFile.exists()) { JasperReport jasperReport1 = (JasperReport) JRLoader .loadObject(reportFile.getPath()); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport1, parameters, con); response.setContentType("application/msword;charset=utf-8"); String fileName = new String("未命名.doc".getBytes("GBK"), "ISO8859_1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); JRExporter exporter = new JRRtfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); exporter.exportReport(); }} catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != con) { try { con.close(); } catch (SQLException e) { con = null; e.printStackTrace(); } } } } }
public void PrintReportData() throws Exception {
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST); Map map = JsonParam.JsonRequest();// 参数 String year = (String) map.get("year"); String month = (String) map.get("month"); String mm = (String)map.get("month"); String areaCode = (String) map.get("areaCode"); String areaName = (String) map.get("areaName"); String flag = (String) map.get("isFaFlag"); if(Integer.parseInt(month) < 10){ month = "0"+month; } StringBuffer SQLSTR = new StringBuffer(); SQLSTR.append("select t3.unit_code, t3.unit_desc1, t4.code_desc"); SQLSTR.append(" from tdc003 t4"); SQLSTR.append(" right join (select t1.unit_code, t1.unit_desc1, t1.unit_type"); SQLSTR.append(" from tdc004 t1"); SQLSTR.append(" left join tdc001 t2"); SQLSTR.append(" on t1.ds_code = t2.ds_code"); if(flag.equals("true")){ SQLSTR.append(" where t2.p_code = '"+areaCode.trim()+"') t3"); }else { SQLSTR.append(" where t2.ds_code = '"+areaCode.trim()+"') t3"); } SQLSTR.append(" on t4.code = t3.unit_type"); SQLSTR.append(" where exists (select 1 from tdc005 t5 where t5.unit_code = t3.unit_code) "); SQLSTR.append(" order by t3.unit_code"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy 年 M 月 d 日"); Map vmap = new HashMap(); vmap.put("PRINTDATE", sdf.format(new Date())); vmap.put("AREANAME", areaName); vmap.put("QUERYDATE", year + "-" + month);// 参数:统计月份 vmap.put("SELDATE", year + " 年 " + mm+" 月 ");// 参数:统计月份 vmap.put("SUBREPORT_DIR", request.getRealPath("/report/") + "/"); ReportHandle.PrintReport("/report/rp_dv01.jasper", "SQLSTR", SQLSTR.toString(), ReportType.PDF, vmap); }