http://wenku.baidu.com/view/78e0ed74a417866fb84a8eca.html
1.Ireport生成图表
下载Ireport3.74
1、 文件-新建
2、 选择Blank A4 、—》 lannch Report Wizard
3、 设定报表名称,生成完报表的存放位置,选择下一步
4、 新建数据源new
5、选择Database JDBC connection
注: 将以下红色框内容按情况填写正确。
注:由于ireport只提供了mysql的dirver,如用oracle数据库需先将驱动jar包导入ireport。
导入方式:工具->选项->CLASSPATH把相关jar包导入。
6、 输入SQL语句 SQL是报表要查询的数据下一步。
注:这块没有给SQL传入参数,传参方法请参考报表生成PPT。
7、 把相应查询出来报表要用到的File添加进去,下一步->完成。
8、 以下band只留Summary Band,其他band鼠标右键Delete Band并且在Summary Band中添加报表。
注:在Detail1中添加报表容易产生重复报表。
9、 将chart直接拖入 Summary Band区域,选择报表下一步。
10、添加需要统计的数据组到第一个对话框,对应组的值放入第二个对话框。选择相应的Field。
注:如没出来定义报表的时候选择的字段需要点击报表Edit Query把相应的sql对应的字read到Fields里面。
11、增加类别
12、增加柱图显示数量
13、OK图表生成了。Preview查看下效果吧。后续编译生成.jasper文件放入工程中。
2.关于撰取
注:其中HyperLink target与HyperLinkType都是需要设置参数,HyperLink Referece Expression
是撰取的url附带参数可以实现分类撰取。
3.Jasper导出代码部分
需要配置一个servlet在web.xml
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>
我们工程需要加入jcommon-1.0.16.jar如需要导出样式加入jasperreports-chart-themes-3.7.4.jar。
代码:
1、公共调用的工具类负责生成并导出图表:
2、对应的Action(撰取)
2.1、被撰取action
3、展示的JSP
3、图表的实例生成文件
4、撰取图表
Action:
- package com.xin.struts2;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import org.apache.struts2.convention.annotation.Action;
- import org.apache.struts2.convention.annotation.Result;
- import com.opensymphony.xwork2.ActionSupport;
- @Action(value = "jasperTest",results={@Result(name="jasper",location="/jasperTest.jsp")})
- public class JasperTestAction extends ActionSupport{
- private StringBuffer vpnbuffer = null;
- public StringBuffer getVpnbuffer() {
- return vpnbuffer;
- }
- public void setVpnbuffer(StringBuffer vpnbuffer) {
- this.vpnbuffer = vpnbuffer;
- }
- private static final String URL = "xxxxxxxxxxxxxxxxxxx";
- public String getHtml(){
- Connection conn = getConn();
- HttpServletRequest request = ServletActionContext.getRequest();
- Map<String,String> parameters = new HashMap<String,String>();
- parameters.put("title", "xxxxxxx");
- parameters.put("title2", "xxxxxx");
- vpnbuffer = new StringBuffer();
- File reportFile = new File(ServletActionContext.getServletContext().getRealPath(
- File.separator + "report" + File.separator + "report1.jasper"));
- try {
- ChartReportUtil.genAndExpChart(vpnbuffer, conn, request, reportFile, parameters);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("sbuffer"+vpnbuffer);
- return "jasper";
- }
- private static Connection getConn(){
- Connection conn = null;
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try {
- conn = DriverManager.getConnection(URL,"xxxxx","xxxxxx");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- public static void main(String[] args) {
- System.out.println(new Date().toGMTString());
- }
- }
util:
- package com.xin.struts2;
- import java.io.File;
- import java.sql.Connection;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JRExporterParameter;
- import net.sf.jasperreports.engine.JRRuntimeException;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperReport;
- import net.sf.jasperreports.engine.export.JRHtmlExporter;
- import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
- import net.sf.jasperreports.engine.util.JRLoader;
- import net.sf.jasperreports.j2ee.servlets.ImageServlet;
- /**
- * 图表工具类
- * @author xxxxx
- *
- */
- @SuppressWarnings("unchecked")
- public class ChartReportUtil {
- //报表图片生成路径
- private static final String REPORT_IMG_DIR = "compReportImg";
- /**
- * 生成并导出图形报表
- * @param buffer output HTML
- * @param conn DB connection used for query
- * @param request
- * @param reportFile report JASPER file
- * @param parameters Used for the parameters of the query
- * @throws JRException
- */
- public static void genAndExpChart(StringBuffer sbuffer,Connection conn,HttpServletRequest request
- ,File reportFile,Map<String,String> parameters) throws Exception{
- try {
- if(!reportFile.exists()){ //Check file exists
- throw new JRRuntimeException("jasper file is not found!");
- }
- if(null == sbuffer){ //Check StringBuffer init
- throw new RuntimeException("StringBuffer is not init");
- }
- JRHtmlExporter exporter = new JRHtmlExporter();
- JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
- JasperPrint jasperPrint = null;
- jasperPrint = JasperFillManager.fillReport(jasperReport,parameters, conn);
- exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
- exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER,sbuffer);
- exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);
- exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
- exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
- request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
- String imgServDirUrl = REPORT_IMG_DIR + File.separator + getFileNameNoEx(reportFile.getName());//构造报表存放子目录,以期解决一次请求返回多个图形报表重叠现象
- File imgRealDir= new File(request.getRealPath(File.separator) + imgServDirUrl);
- if(!imgRealDir.exists()) {
- imgRealDir.mkdirs();
- }
- //重设JRHtmlExporterParameter.IMAGES_URI
- exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() +"/"+imgServDirUrl+ "/");
- exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR, imgRealDir);
- exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
- exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
- exporter.exportReport();
- }finally{
- if(conn!=null){
- conn.close();
- }
- }
- }
JSP:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'login.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <form action="jasperTest!getHtml.action" method="post">
- <input type="submit" value="生成图表">
- </form>
- <table border="0" cellpadding="0" cellspacing="0" align="left">
- <tr>
- <td align="center" colspan=2>
- <font size="3">${vpnbuffer}</font>
- </td>
- </tr>
- </tr>
- </c:if>
- </table>
- </body>
- </html>
WEB.XML
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>Struts2Project</display-name>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <span style="color:#ff0000;"> </span> <servlet>
- <servlet-name>ImageServlet</servlet-name>
- <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ImageServlet</servlet-name>
- <url-pattern>/image</url-pattern>
- </servlet-mapping>
- </web-app>
4.效果图
5.生成的HTML
<a name="JR_PAGE_ANCHOR_0_1"></a>
<table style="width: 800px; border-collapse: collapse; empty-cells: show" cellpadding="0" cellspacing="0" border="0" bgcolor="white">
<tr>
<td style="width: 20px; height: 1px;"></td>
<td style="width: 760px; height: 1px;"></td>
<td style="width: 20px; height: 1px;"></td>
</tr>
<tr valign="top">
<td colspan="3" style="width: 800px; height: 20px;"></td>
</tr>
<tr valign="top">
<td style="width: 20px; height: 217px;"></td>
<td><img src="image?image=img_0_0_0" style="height: 217px" usemap="#map_0_0_0" alt="" border="0" title="hyperLink!getHtml.action"/>
<map name="map_0_0_0">
<area shape="poly" coords="371,33,380,33,380,102,380,102" href="hyperLink!getHtml.action?province=HKG"/>
<area shape="poly" coords="363,35,371,33,380,102,380,102" href="hyperLink!getHtml.action?province=HEB"/>
<area shape="poly" coords="324,61,332,52,341,44,352,38,363,35,380,102,380,102" href="hyperLink!getHtml.action?province=JSU"/>
<area shape="poly" coords="316,76,324,61,380,102,380,102" href="hyperLink!getHtml.action?province=CHQ"/>
<area shape="poly" coords="313,83,316,76,380,102,380,102" href="hyperLink!getHtml.action?province=HUB"/>
<area shape="poly" coords="311,91,313,83,380,102,380,102" href="hyperLink!getHtml.action?province=GUD"/>
<area shape="poly" coords="311,108,311,91,380,102,380,102" href="hyperLink!getHtml.action?province=HEN"/>
<area shape="poly" coords="314,124,311,108,380,102,380,102" href="hyperLink!getHtml.action?province=GAN"/>
<area shape="poly" coords="321,138,314,124,380,102,380,102" href="hyperLink!getHtml.action?province=BEJ"/>
<area shape="poly" coords="326,145,321,138,380,102,380,102" href="hyperLink!getHtml.action?province=GXI"/>
<area shape="poly" coords="332,151,326,145,380,102,380,102" href="hyperLink!getHtml.action?province=GUI"/>
<area shape="poly" coords="380,33,391,34,402,36,412,41,421,47,430,54,436,63,442,73,446,83,448,95,448,106,447,117,443,127,438,137,432,146,424,154,415,161,404,166,393,169,382,170,371,170,361,168,350,164,340,158,332,151,380,102,380,102" href="hyperLink!getHtml.action?province=JUC"/>
<area shape="default" coords="0,0,760,217" nohref="nohref" title="hyperLink!getHtml.action"/>
</map>
</td>
<td style="width: 20px; height: 217px;"></td>
</tr>
<tr valign="top">
<td style="width: 20px; height: 343px;"></td>
<td><img src="image?image=img_0_0_1" style="height: 343px" usemap="#map_0_0_1" alt="" border="0"/>
<map name="map_0_0_1">
<area shape="rect" coords="587,264,622,270" href="hyperLink!getHtml.action?province=HKG"/>
<area shape="rect" coords="540,264,575,270" href="hyperLink!getHtml.action?province=HEB"/>
<area shape="rect" coords="493,232,528,270" href="hyperLink!getHtml.action?province=JSU"/>
<area shape="rect" coords="446,258,481,270" href="hyperLink!getHtml.action?province=CHQ"/>
<area shape="rect" coords="399,264,434,270" href="hyperLink!getHtml.action?province=HUB"/>
<area shape="rect" coords="353,264,388,270" href="hyperLink!getHtml.action?province=GUD"/>
<area shape="rect" coords="306,258,341,270" href="hyperLink!getHtml.action?province=HEN"/>
<area shape="rect" coords="259,258,294,270" href="hyperLink!getHtml.action?province=GAN"/>
<area shape="rect" coords="212,258,247,270" href="hyperLink!getHtml.action?province=BEJ"/>
<area shape="rect" coords="165,264,200,270" href="hyperLink!getHtml.action?province=GXI"/>
<area shape="rect" coords="118,264,153,270" href="hyperLink!getHtml.action?province=GUI"/>
<area shape="rect" coords="71,61,106,270" href="hyperLink!getHtml.action?province=JUC"/>
</map>
</td>
<td style="width: 20px; height: 343px;"></td>
</tr>
<tr valign="top">
<td colspan="3" style="width: 800px; height: 20px;"></td>
</tr>
</table>