欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

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

更新时间: 2014-01-05 03:07:24 责任编辑: Author_N1

 

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();
		}
	}

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 03:07:24
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/18595/
本文WWW.DOC100.NET DOC100.NET版权所有。