java调ireport---(poi流输出word)(2次试验)

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		//*String username = request.getParameter("username");   
		

		SQLConn sqlConn = new SQLConn();
		try {
			Connection conn = sqlConn.getConnection1();
			String root_path = this.getServletContext().getRealPath("");
			String reportFilePath = root_path
					+ "\\WEB-INF\\classes\\com\\ztesoft\\bidding\\contract\\jspreport\\javaBean.jasper ";
			File reportFile1 = new File(reportFilePath);
			if (!reportFile1.exists())
				throw new JRRuntimeException(
						"没找到文件路径");
			JasperReport jasperReport1 = (JasperReport) JRLoader
					.loadObject(reportFile1.getPath());
			//Map parameters = new HashMap();
			//parameters.put("id", 1);
			
			
			
			//-------数据传输-------------
			List<testVo> data = new ArrayList<testVo>();
			testVo TestVo = new testVo();

			List<testVo> list = new ArrayList<testVo>();
			try {
				Statement stmt = conn.createStatement();
				ResultSet rslt = stmt
						.executeQuery("select * from PARTNER where partner_id = 2843");
				while (rslt.next()) {
					testVo obj = new testVo();
					obj.setContactMan(rslt.getString("CONTACT_MAN"));
					obj.setContactTel(rslt.getString("contact_Tel"));
					obj.setBank(rslt.getString("bank"));
					obj.setAccount(rslt.getString("account"));
					list.add(obj);
				}

				for (int i = 0; i < list.size(); i++) {
					TestVo.setContactMan(list.get(i).getContactMan());
					TestVo.setContactTel(list.get(i).getContactTel());
					TestVo.setBank(list.get(i).getBank());
					TestVo.setAccount(list.get(i).getAccount());
				}
				//假数据
				//数量 budget_amount  单价 price 
				TestVo.setBudgetAmount("10");
				TestVo.setPrice("9300.000");
				//--商品信息  名称 name  商品型号material_type
				TestVo.setName("联想电脑XM300");
				TestVo.setMaterialType("1颗2.40GHz四核XEON处理器,2GB内存");
				// 交货期 2交货地点 联系人
			} catch (SQLException e) {
				e.printStackTrace();
			}

			data.add(TestVo);
			JRDataSource dataSource = new JRBeanCollectionDataSource(data);
			//-------数据传输-------------------

			
			
			JasperPrint jasperPrint1 = JasperFillManager.fillReport(
					jasperReport1, null, dataSource);

			JRExporter exporter = null;
			exporter = new JRRtfExporter();
			List reportlist = (List) new java.util.ArrayList();
			reportlist.add(jasperPrint1);
			System.out.println(reportlist.size() + "list");
			byte[] bytes;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,
					reportlist);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);

			//qijia 
			exporter.setParameter(JRExporterParameter.JASPER_PRINT,
					jasperPrint1);
			//exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);

			exporter.exportReport();
			// exporter.exportReportToStream();
			bytes = baos.toByteArray();
			// System.out.println(bytes+"bytes");
			if (bytes != null && bytes.length > 0) {
				System.out.println(bytes+"bytes");
				try {
					//response.reset();
					response.setContentType("application/ms_word");
					response.setHeader("Content-disposition",
							"attachment; filename=yourReport.doc");
					response.setContentLength(bytes.length);
					ServletOutputStream ouputStream = response
							.getOutputStream();
					ouputStream.write(bytes, 0, bytes.length);
					ouputStream.flush();
					

					ouputStream.close();
					// response.reset();

				} catch (Exception ex) {
					ex.printStackTrace();
				} finally {
					conn.close();
				}
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

你可能感兴趣的:(java,Web)