Jasperreport5.6.0生成的报表,可有多种格式,这里主要讲生成 PDF,RTF,EXCEL,HTML,XML等格式,这里讲生成EXCEL格式的,在JSP页面写代码,如下所示:
<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>
"net.sf.jasperreports.engine.*" "net.sf.jasperreports.engine.export.*"%>
"org.springframework.ui.jasperreports.JasperReportsUtils""java.util.*" "java.io.*" "java.sql.*" <%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
File jasperFile=new File("D:/Workspaces/zwjxSystem/WebRoot/jasper/report1.jasper");
String url ="jdbc:oracle:thin:@192.168.1.104:1521:orcl";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn =DriverManager.getConnection(url,"zwjx",255); font-family:'Courier New'">"zwjx");
String projectapproid =request.getParameter("projectapproid");
Map map = new HashMap();
map.put("projectapproid",projectapproid);
//打印
JasperPrintprint = JasperFillManager.fillReport(jasperFile.getPath(),map,conn);
OutputStream os=response.getOutputStream();
StringfileNames="项目立项";
JRAbstractExporter exporter = new JRXmlExporter();
response.reset();
response.setHeader("Content-Disposition",255); font-family:'Courier New'">"attachment;filename="+new String(fileNames.getBytes("gbk"),255); font-family:'Courier New'">"iso8859-1")+".xml");
JasperReportsUtils.render(exporter,print,os);
os.flush();
os.close();
%>
运行后,弹出如下对话框,我们可以根据需要选择。可以直接打开,也可以保存。