Jasperreport5.6.0生成的报表,可有多种格式,这里主要讲生成 PDF,RTF,EXCEL,HTML,XML等格式,这里讲生成RTF格式的,在JSP页面写代码,如下所示:
<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*"%>
<%@ page import="org.springframework.ui.jasperreports.JasperReportsUtils"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
//报表生成之后编译生成的.jasper文件位置
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","zwjx");
String projectapproid =request.getParameter("projectapproid");
System.out.println(projectapproid);
Map map = new HashMap();
map.put("projectapproid",projectapproid);
//打印
JasperPrint print = JasperFillManager.fillReport(jasperFile.getPath(), map,conn);
OutputStream os=response.getOutputStream();
//生成文件名
String fileNames="项目立项";
JRAbstractExporter exporter = new JRRtfExporter();
response.reset();
response.setContentType("application/msword");
response.setHeader("Content-Disposition","attachment;filename="+new String(fileNames.getBytes("gbk"),"iso8859-1")+".rtf");
JasperReportsUtils.render(exporter,print,os);
os.flush();
os.close();
%>
运行后,弹出如下对话框,我们可以根据需要选择。可以直接打开,也可以保存。