81,094
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%
JasperPrint jasperPrint = null;
Connection sqlCon;
String strCon;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
strCon = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=myDatabase";
sqlCon = DriverManager.getConnection(strCon, "sa", "");
File reportFile = new File(application.getRealPath("/jasper/classic.jasper"));
Map parameters = new HashMap();
parameters.put("ReportTitle", "p");
//使用SQL查询
try {
jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, sqlCon);//填充报表数据生成JasperPrint对象
JasperPrintManager.printReport(jasperPrint, false);//2008-08-25 直接打印,不用预览PDF直接打印 true为弹出打印机选择.false为直接打印.
//输出
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);//将JasperPrint对象写入对象输出流中
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
//response.reset();
out.clear();
out = pageContext.pushBody();
} catch (JRException jre) {
jre.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
}
response.setContentType("text/html");
response.sendRedirect("index.jsp");
%>
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<input name="Submit" type="submit" class="font12g18h" value="打 印" onclick="Javascript:printit()"/>
<script language="javascript">
function printsetup(){
// 打印页面设置
wb.execwb(8,1);
}
function printpreview(){
// 打印页面预览
wb.execwb(7,1);
}
function printit(){
if (confirm("确定打印吗?")) {
wb.execwb(6,6)
}
}
</script>