JFreeReport的web报表的实现

         HtmlExampleServlet演示如何通过HTML表格在Web环境下展示JFreeReport定义的表格
  简单报表的HTML展示
JFreeReport提供HtmlProcessor类来将输入的JFreeReport报表变成HTML表格形式,最后通过Servlet的输出通道展示给用户,具体的用法如下例:
 

 

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.report.JFreeReport;
import org.jfree.report.modules.output.table.base.TableProcessor;
import org.jfree.report.modules.output.table.html.HtmlProcessor;
import org.jfree.report.modules.output.table.html.StreamHtmlFilesystem;
import org.jfree.report.util.Log;





/**
 * 
 * <p>HtmlExampleServlet演示如何通过HTML表格在Web环境下展示JFreeReport定义的表格
 *  简单报表的HTML展示
JFreeReport提供HtmlProcessor类来将输入的JFreeReport报表变成HTML表格形式,最后通过Servlet的输出通道展示给用户,具体的用法如下例:
* 
 * <p>
 *
 * 创建日期 2013-5-13<br>
 * @author  longgangbai<br>
 * @version $Revision$ 2013-5-13
 * @since   3.0.0
 */
public class JFreeReportHtmlServlet extends HttpServlet {
    //
    private static final long serialVersionUID = 1L;

    public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
            IOException {
        doPost(request, response);
    }

    public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
            IOException {
        TableProcessor worker;
        // 设置输出页面的Meta信息
        response.setHeader("Content-Disposition", "inline; filename=\"" + "unknown.html" + "\"");
        // 设置页面的输出格式
        response.setHeader("Content-Type", "text/html");
        try {
            // 获取Servlet的输出流
            OutputStream out = response.getOutputStream();
            // 获取HelloWorld中定义的报表
            JFreeReportView jfreeReport =new  JFreeReportView();
            JFreeReport report=jfreeReport.getJFreeReport();
            
            // 将报表和Servlet的输出流作为参数传递给TableProcessor的实例
            final HtmlProcessor processor = new HtmlProcessor(report);
            processor.setFilesystem(new StreamHtmlFilesystem(out));
            worker = processor;
        } catch (Exception e) {
            Log.debug("无法解析报表定义", e);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        try {
            // 输出结果
            worker.processReport();
        } catch (Exception e) {
            Log.debug("输出结果产生错误", e);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
    }
}

 

 

动态的JFreeReport报表实现:

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.jfree.report.ElementAlignment;
import org.jfree.report.JFreeReport;
import org.jfree.report.ReportProcessingException;
import org.jfree.report.elementfactory.TextFieldElementFactory;
import org.jfree.report.modules.gui.base.PreviewDialog;
import org.jfree.ui.FloatDimension;

/**
 * 使用JFreeReport生成报表的简单例子,用于演示使用JFreeReport生成报表的一些基本步骤
 * 
 * 本例子中,为了简化操作,报表定义是使用java直接编码
 *
 * @ 作者 : bookman
 */
public class JFreeReportView {
    /**
     * 处理窗口关闭事件
     */
    protected static class CloseHandler extends WindowAdapter {
        public void windowClosing(final WindowEvent event) {
            System.exit(0);
        }
    }

    /**
     * 创建和显示简单的报表
     */
    public JFreeReportView() {
    }

    public JFreeReport getJFreeReport() {
        // 获得创建报表需要用到的数据
        final TableModel data = createData();
        // 获得报表要用到的报表定义内容
        final JFreeReport report = createReportDefinition();
        // 将报表定义和数据结合
        report.setData(data);
        return report;
    }

    public  void viewJFreeReport() {
        try {
            JFreeReport report =getJFreeReport();
            
            // 将生成的报表放到预览窗口中
            final PreviewDialog preview = new PreviewDialog(report);
            preview.addWindowListener(new CloseHandler());
            preview.pack();
            // 显示报表预览窗口
            preview.setVisible(true);
        } catch (ReportProcessingException e) {
            System.out.println(e);
        }
    }

    /**
     * 创建生成报表需要用到的数据
     *
     * @返回一个TableModel实例
     */
    private TableModel createData() {
        final Object[] columnNames = new String[] {"Column1", "Column2" };
        final DefaultTableModel result = new DefaultTableModel(columnNames, 100);
        int rownum = 0;
        for (; rownum < 100; rownum++) {
            result.setValueAt("say Hello " + rownum + "次", rownum, 0);
            result.setValueAt("say World " + rownum + "次", rownum, 1);
        }
        return result;
    }

    /**
     * 创建一个报表定义
     *
     * @返回一个报表定义实例
     */
    private JFreeReport createReportDefinition() {
        final JFreeReport report = new JFreeReport();
        report.setName("A Very Simple Report");
        /**
         * 定义要显示报表第一列的样式
         */
        TextFieldElementFactory factory = new TextFieldElementFactory();
        factory.setName("T1");
        factory.setAbsolutePosition(new Point2D.Float(0, 0));
        factory.setMinimumSize(new FloatDimension(150, 20));
        factory.setColor(Color.black);
        factory.setHorizontalAlignment(ElementAlignment.LEFT);
        factory.setVerticalAlignment(ElementAlignment.MIDDLE);
        factory.setNullString("-");
        factory.setFieldname("Column1");
        report.getItemBand().addElement(factory.createElement());
        /**
         * 定义要显示报表第二列的样式
         */
        factory = new TextFieldElementFactory();
        factory.setName("T2");
        factory.setAbsolutePosition(new Point2D.Float(200, 0));
        factory.setMinimumSize(new FloatDimension(150, 20));
        factory.setColor(Color.black);
        factory.setHorizontalAlignment(ElementAlignment.LEFT);
        factory.setVerticalAlignment(ElementAlignment.MIDDLE);
        factory.setNullString("-");
        factory.setFieldname("Column2");
        report.getItemBand().addElement(factory.createElement());
        /**
         * 返回一个报表定义的实例
         */
        return report;
    }

}

 

你可能感兴趣的:(动态报表的实现,JFreeReport)