日期:2014-05-17 浏览次数:20951 次
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ page import="java.sql.*"%> <%@ page import="java.io.*"%> <%@ page import="net.sf.jasperreports.engine.JasperFillManager"%> <%@ page import="net.sf.jasperreports.engine.JasperPrint"%> <%@ page import="net.sf.jasperreports.engine.JRException"%> <%@ page import="net.sf.jasperreports.engine.JRExporterParameter"%> <%@ page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%> <%@ page import="net.sf.jasperreports.engine.export.JRXlsExporter"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String Name = "aa";//初始化变量 Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","lshh","lshh"); //装载jasper文件application File exe_rpt = new File(application.getRealPath("/test.jasper")); //rowid就是iReport的变量$P{rowid}的名称 Map parameters = new HashMap(); parameters.put("Name", Name); try { // fill JasperPrint jasperPrint = JasperFillManager.fillReport(exe_rpt .getPath(), parameters, conn); // excel输出 ByteArrayOutputStream oStream = new ByteArrayOutputStream(); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream); exporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除记录最下面的空行 exporter.setParameter( JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);// 删除多余的ColumnHeader exporter.setParameter( JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);// 显示边框 exporter.exportReport(); byte[] bytes = oStream.toByteArray(); if (bytes != null && bytes.length > 0) { response.reset(); response.setContentType("application/vnd.ms-excel"); response.setContentLength(bytes.length); ServletOutputStream ouputStream = response .getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); } else { out.print("bytes were null!"); } conn.close(); } catch (JRException ex) { out.print("Jasper Output Error:" + ex.getMessage()); } %> </body> </html>