📄 jfreereportexcelservlet.java
字号:
/** * ======================================== * JFreeReport : a free Java report library * ======================================== * * Project Info: http://www.jfree.org/jfreereport/index.html * Project Lead: Thomas Morgner; * * (C) Copyright 2000-2003, by Simba Management Limited and Contributors. * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. * * ------------------------------ * JFreeReportExcelServlet.java * ------------------------------ * (C)opyright 2003, by Thomas Morgner and Contributors. * * Original Author: Thomas Morgner; * Contributor(s): David Gilbert (for Simba Management Limited); * * $Id: JFreeReportExcelServlet.java,v 1.2.2.1 2003/07/16 18:46:52 taqua Exp $ * * Changes * ------------------------- * 16.07.2003 : Initial version * */package com.jrefinery.report.ext.demo;import java.io.IOException;import java.net.URL;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.ServletException;import com.jrefinery.report.util.Log;import com.jrefinery.report.targets.table.excel.ExcelProcessor;public class JFreeReportExcelServlet extends HttpServlet{ /** * Handles the GET method for the servlet. The GET method is mapped to * the POST method, both commands are handled equal. * * @param request the http request object. * @param response the http response object. * @throws javax.servlet.ServletException if an error occured, which could not be handled internaly. * @throws java.io.IOException if writing the generated contents failed. */ public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * Handles the POST method for the request. This parses the report definition, * loads the tablemodel and generates a single page of the report. The generated * page is returned as Excel file, with the implicit restriction, that images * are not included. * * @param request the http request object. * @param response the http response object. * @throws javax.servlet.ServletException if an error occured, which could not be handled internaly. * @throws java.io.IOException if writing the generated contents failed. */ public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { Log.debug("in processRequest..." + getClass()); final URL in = getClass().getResource("/com/jrefinery/report/demo/swing-icons.xml"); if (in == null) { throw new ServletException("Missing Resource: /com/jrefinery/report/demo/swing-icons.xml"); } final URL base = getServletContext().getResource("/WEB-INF/lib/jlfgr-1_0.jar"); Log.debug("Base: " + base); final AbstractTableReportServletWorker worker = new DefaultTableReportServletWorker(in, new DemoModelProvider(base)); // display the content in the browser window (see RFC2183) // may or may not work for excel content ... response.setHeader("Content-Disposition", "inline; filename=\"" + "unknown.xls" + "\""); response.setHeader("Content-Type", "application/vnd.ms-excel"); try { // this throws an exception if the report could not be parsed final ExcelProcessor processor = new ExcelProcessor(worker.getReport()); processor.setOutputStream(response.getOutputStream()); worker.setTableProcessor(processor); } catch (Exception e) { Log.debug("Failed to parse the report", e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } //response.setHeader("Content-Disposition", "attachment; filename=\"" + "unknown.pdf" + "\""); //above line if enabled will pop-Out the browsers "File Download" dialog //with the standard options: "Open from current location"/ "Save to disk" try { worker.processReport(); } catch (Exception e) { Log.debug("Failed to create the report", e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -