Posted on 2005-01-07 18:24 
海天一鸥 阅读(465) 
评论(0)  编辑  收藏  所属分类: 
报表专题 
			 
			
		 
		  <%@ page contentType="text/html;charset=GBK" %>
<%@ page contentType="text/html;charset=GBK" %> 
 <%@ page import="dori.jasper.engine.*" %>
<%@ page import="dori.jasper.engine.*" %> 
 <%@ page import="java.util.*" %>
<%@ page import="java.util.*" %> 
 <%@ page import="java.io.*" %>
<%@ page import="java.io.*" %> 
 <%@ page import="java.sql.*" %>
<%@ page import="java.sql.*" %> 
 <%@ page import="com.zx.report.util.CarUse"%>
<%@ page import="com.zx.report.util.CarUse"%> 
 <%@ page import="dori.jasper.engine.data.*" %>
<%@ page import="dori.jasper.engine.data.*" %> 
 <%
<% 
 Connection conn = null;
Connection conn = null; 
 Statement st = null;
Statement st = null; 
 ResultSet rs = null;
ResultSet rs = null; 
 byte[] bytes = null;
byte[] bytes = null; 
 List carUseList=new ArrayList();
List carUseList=new ArrayList(); 
 
 

 try
try {
{ 
 
 
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
 conn =DriverManager.getConnection("jdbc:microsoft:sqlserver://10.10.10.207:1433;DatabaseName=report;user=sa;password=111111");
conn =DriverManager.getConnection("jdbc:microsoft:sqlserver://10.10.10.207:1433;DatabaseName=report;user=sa;password=111111"); 
 
 
 //以输入流方式
//以输入流方式 
 InputStream reportFile = getClass().getClassLoader().getResourceAsStream("carUserBd.jasper");
   InputStream reportFile = getClass().getClassLoader().getResourceAsStream("carUserBd.jasper"); 
 
 
 //以reportFile方式
//以reportFile方式 
 //File reportFile =new File(request.getRealPath("/report/carUserBd.jasper"));
//File reportFile =new File(request.getRealPath("/report/carUserBd.jasper")); 
 
 
 Map parameters = new HashMap();
Map parameters = new HashMap(); 
 
 
 
 
 String sqlCarUser="SELECT car.carNo, dept.Name , people.peopleName,carUse.useDescription ," +
String sqlCarUser="SELECT car.carNo, dept.Name , people.peopleName,carUse.useDescription ," + 
 " carUse.beginTime , carUse.planEndTime , carUse.comment , carUse.driver," +
" carUse.beginTime , carUse.planEndTime , carUse.comment , carUse.driver," + 
 " carUse.peopleNumber ,OneWay= case carUse.isOneWay  when 0 then '双程'  else '单程'  end, " +
" carUse.peopleNumber ,OneWay= case carUse.isOneWay  when 0 then '双程'  else '单程'  end, " + 
 "isFirst= case carUse.priority when 0  then '低' when  1  then  '中'  else  '高'  end, carUse.start , " +
"isFirst= case carUse.priority when 0  then '低' when  1  then  '中'  else  '高'  end, carUse.start , " + 
 "carUse.destination , people.peopleName as ratifier FROM DEPT_CARUSELOG carUse , DEPT_CARINFO car , " +
"carUse.destination , people.peopleName as ratifier FROM DEPT_CARUSELOG carUse , DEPT_CARINFO car , " + 
 "ADMIN_ORGANIZATION dept , PUBLIC_PEOPLEINFO people WHERE carUse.id=290 and " +
"ADMIN_ORGANIZATION dept , PUBLIC_PEOPLEINFO people WHERE carUse.id=290 and " + 
 "caruse.carId=car.carId and carUse.departId=dept.id and carUse.peopleId=people.peopleId";
"caruse.carId=car.carId and carUse.departId=dept.id and carUse.peopleId=people.peopleId"; 
 
 
 st = conn.createStatement();
st = conn.createStatement(); 
 rs = st.executeQuery(sqlCarUser);
rs = st.executeQuery(sqlCarUser); 
 
 

 while(rs.next())
while(rs.next()) {
{ 
 CarUse carUse=new CarUse();
CarUse carUse=new CarUse(); 
 carUse.setCarNo(rs.getString(1));
carUse.setCarNo(rs.getString(1)); 
 carUse.setName(rs.getString(2));
carUse.setName(rs.getString(2)); 
 carUse.setPeopleName(rs.getString(3));
carUse.setPeopleName(rs.getString(3)); 
 carUse.setUseDescription(rs.getString(4));
carUse.setUseDescription(rs.getString(4)); 
 carUse.setBeginTime(rs.getDate(5));
carUse.setBeginTime(rs.getDate(5)); 
 carUse.setPlanEndTime(rs.getDate(6));
carUse.setPlanEndTime(rs.getDate(6)); 
 carUse.setComment(rs.getString(7));
carUse.setComment(rs.getString(7)); 
 carUse.setDriver(rs.getString(8));
carUse.setDriver(rs.getString(8)); 
 carUse.setPeopleNumber(rs.getString(9));
carUse.setPeopleNumber(rs.getString(9)); 
 carUse.setOneWay(rs.getString(10));
carUse.setOneWay(rs.getString(10)); 
 carUse.setIsFirst(rs.getString(11));
carUse.setIsFirst(rs.getString(11)); 
 carUse.setStart(rs.getString(12));
carUse.setStart(rs.getString(12)); 
 carUse.setDestination(rs.getString(13));
carUse.setDestination(rs.getString(13)); 
 carUse.setRatifier(rs.getString(14));
carUse.setRatifier(rs.getString(14)); 
 
 
 carUseList.add(carUse);
carUseList.add(carUse); 
 
 
 }
} 
 
 
 JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(carUseList);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(carUseList); 
 
 
 bytes =
bytes = 
 JasperRunManager.runReportToPdf(
JasperRunManager.runReportToPdf( 
 reportFile,
reportFile, 
 parameters,
parameters, 
 ds);
ds); 
 String fileName = "test.pdf";
String fileName = "test.pdf"; 
 response.setContentType("application/pdf");
response.setContentType("application/pdf"); 
 response.addHeader(
response.addHeader( 
 "Content-Disposition",
"Content-Disposition", 
 "attachment;filename=\"" + fileName);
"attachment;filename=\"" + fileName); 
 
 
 response.setContentLength(bytes.length);
response.setContentLength(bytes.length); 
 ServletOutputStream ouputStream = response.getOutputStream();
ServletOutputStream ouputStream = response.getOutputStream(); 
 
 
 System.out.println("5");
System.out.println("5"); 
 ouputStream.write(bytes, 0, bytes.length);
ouputStream.write(bytes, 0, bytes.length); 
 ouputStream.flush();
ouputStream.flush(); 
 ouputStream.close();
ouputStream.close(); 
 
 

 } catch (SQLException sqle)
} catch (SQLException sqle)  {
{ 
 
 
 System.out.println("SQLException:" + sqle.getMessage());
System.out.println("SQLException:" + sqle.getMessage()); 
 
 

 } catch (JRException e)
} catch (JRException e)  {
{ 
 e.printStackTrace();
e.printStackTrace(); 

 }catch(ClassNotFoundException cnfe)
}catch(ClassNotFoundException cnfe) {
{ 
 
 
 
 
 }
} 
 
 
 %>
%> 
 jasperreport可以用Collection做为数据源,这种方式比用Connection方式更为灵活方便
jasperreport可以用Collection做为数据源,这种方式比用Connection方式更为灵活方便 
 
  