日期:2014-05-17 浏览次数:20813 次
public String branchExcelReport(List list) { JasperPrint jasperPrintArr=null; ArrayList jasperPrintList = new ArrayList(); HttpServletResponse resp=null; JasperReport report; Map map=new HashMap(); String realPath = ""; resp = (HttpServletResponse)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE); realPath =ServletActionContext.getServletContext().getRealPath( "/report/template/Branch.jasper"); JRDataSource dataSource = new JRBeanCollectionDataSource(list); try { report = (JasperReport)JRLoader.loadObject(realPath); jasperPrintArr = JasperFillManager.fillReport (report, map, dataSource); jasperPrintList.add(jasperPrintArr); //设定输出格式 FileBufferedOutputStream fbos = new FileBufferedOutputStream(); // 使用JRPdfExproter导出器导出pdf JRPdfExporter exporter = new JRPdfExporter(); // 设置JasperPrintList exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.exportReport(); resp.reset(); resp.setContentType("application/vnd.ms-excel;charset=UTF-8"); resp.setCharacterEncoding("UTF-8"); resp.setContentLength(fbos.size()); resp.setHeader("Content-Disposition", "attachment; filename=BRANCH.xls"); ServletOutputStream ouputStream = resp.getOutputStream(); try { fbos.writeData(ouputStream); fbos.dispose(); ouputStream.flush(); } finally { if (ouputStream != null) { try { fbos.close(); ouputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }