|
1<%@ page contentType="text/html;charset=GBK" %> 2<%@ page import="dori.jasper.engine.*" %> 3<%@ page import="java.util.*" %> 4<%@ page import="java.io.*" %> 5<%@ page import="java.sql.*" %> 6<%@ page import="com.zx.report.util.CarUse"%> 7<%@ page import="dori.jasper.engine.data.*" %> 8<% 9Connection conn = null; 10Statement st = null; 11ResultSet rs = null; 12byte[] bytes = null; 13List carUseList=new ArrayList(); 14 15try{ 16 17Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 18conn =DriverManager.getConnection("jdbc:microsoft:sqlserver://10.10.10.207:1433;DatabaseName=report;user=sa;password=111111"); 19 20//以输入流方式 21 InputStream reportFile = getClass().getClassLoader().getResourceAsStream("carUserBd.jasper"); 22 23//以reportFile方式 24//File reportFile =new File(request.getRealPath("/report/carUserBd.jasper")); 25 26Map parameters = new HashMap(); 27 28 29String sqlCarUser="SELECT car.carNo, dept.Name , people.peopleName,carUse.useDescription ," + 30" carUse.beginTime , carUse.planEndTime , carUse.comment , carUse.driver," + 31" carUse.peopleNumber ,OneWay= case carUse.isOneWay when 0 then '双程' else '单程' end, " + 32"isFirst= case carUse.priority when 0 then '低' when 1 then '中' else '高' end, carUse.start , " + 33"carUse.destination , people.peopleName as ratifier FROM DEPT_CARUSELOG carUse , DEPT_CARINFO car , " + 34"ADMIN_ORGANIZATION dept , PUBLIC_PEOPLEINFO people WHERE carUse.id=290 and " + 35"caruse.carId=car.carId and carUse.departId=dept.id and carUse.peopleId=people.peopleId"; 36 37st = conn.createStatement(); 38rs = st.executeQuery(sqlCarUser); 39 40while(rs.next()){ 41CarUse carUse=new CarUse(); 42carUse.setCarNo(rs.getString(1)); 43carUse.setName(rs.getString(2)); 44carUse.setPeopleName(rs.getString(3)); 45carUse.setUseDescription(rs.getString(4)); 46carUse.setBeginTime(rs.getDate(5)); 47carUse.setPlanEndTime(rs.getDate(6)); 48carUse.setComment(rs.getString(7)); 49carUse.setDriver(rs.getString(8)); 50carUse.setPeopleNumber(rs.getString(9)); 51carUse.setOneWay(rs.getString(10)); 52carUse.setIsFirst(rs.getString(11)); 53carUse.setStart(rs.getString(12)); 54carUse.setDestination(rs.getString(13)); 55carUse.setRatifier(rs.getString(14)); 56 57carUseList.add(carUse); 58 59} 60 61JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(carUseList); 62 63bytes = 64JasperRunManager.runReportToPdf( 65reportFile, 66parameters, 67ds); 68String fileName = "test.pdf"; 69response.setContentType("application/pdf"); 70response.addHeader( 71"Content-Disposition", 72"attachment;filename=\"" + fileName); 73 74response.setContentLength(bytes.length); 75ServletOutputStream ouputStream = response.getOutputStream(); 76 77System.out.println("5"); 78ouputStream.write(bytes, 0, bytes.length); 79ouputStream.flush(); 80ouputStream.close(); 81 82} catch (SQLException sqle) { 83 84System.out.println("SQLException:" + sqle.getMessage()); 85 86} catch (JRException e) { 87e.printStackTrace(); 88}catch(ClassNotFoundException cnfe){ 89 90 91} 92 93%> 94 95
jasperreport可以用Collection做为数据源,这种方式比用Connection方式更为灵活方便
|