Java tutorial
/*$Id: JasperReportsXlsViewToFile.java 9125 2008-03-16 23:07:38Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2007 ABM-utvikling * * * * Created on 19-June-2007 * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program 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 General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.common.jasperreports; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.util.Map; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JasperPrint; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.view.jasperreports.JasperReportsXlsView; import org.springframework.ui.jasperreports.JasperReportsUtils; import org.springframework.util.StopWatch; /** * JasperReportsXlsViewToFile. * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author $Author: jens $ * @version $Rev: 9125 $ * $Date: 2008-03-17 00:07:38 +0100 (Mon, 17 Mar 2008) $ * copyright ABM-Utvikling * @since 2007-06-20 */ public class JasperReportsXlsViewToFile extends JasperReportsXlsView { private static final Log log = (Log) LogFactory.getLog(JasperReportsXlsViewToFile.class); /** * Initial size for the output array. */ private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE = 4096; private String outPutFileName = "/tmp/temp_korrektur.xls"; /** * Perform rendering for a single Jasper Reports exporter, that is, * for a pre-defined output format. */ protected void renderReport(JasperPrint populatedReport, Map model, HttpServletResponse response) throws Exception { log.info(" XXXXXXXXXXXXXXXXXXX renderReport: START "); StopWatch stopWatch = new StopWatch(); stopWatch.start("renderReport"); // Prepare report for rendering. JRExporter exporter = createExporter(); if (getConvertedExporterParameters() != null) { exporter.setParameters(getConvertedExporterParameters()); } // We need to write binary output to the response OutputStream. // Render report into local OutputStream. // IE workaround: write into byte array first. ByteArrayOutputStream baos = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE); JasperReportsUtils.render(exporter, populatedReport, baos); // Flush byte array to servlet output stream. java.io.FileOutputStream fileOutputStream = new FileOutputStream(getOutPutFileName()); baos.writeTo(fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); stopWatch.stop(); log.info("[private:renderReport] tok[" + stopWatch.getTotalTimeMillis() + "] ms"); log.info(" XXXXXXXXXXXXXXXXXXX renderReport: FINISH "); } public String getOutPutFileName() { return outPutFileName; } public void setOutPutFileName(String outPutFileName) { this.outPutFileName = outPutFileName; } }