第一次用ireport 怎么建个excel啊
第一次用ireport 怎么建个excel啊
谁能教教我啊 我q 279773998
[解决办法]
/** * 生成報表的公用方法 * @param result * @param jrxmlName * @throws Exception */ public void executeReportCommon(Collection<?> result,String name,String rptOutputPath,String jrxmlName,HttpServletResponse resp){ try{ System.out.println(result.size()); if(result!=null && !result.isEmpty()){ String reportName=""; reportName=name+".xls"; System.out.println(rptOutputPath); System.out.println(jrxmlName); this.exportXls(result, jrxmlName, rptOutputPath, reportName,resp); } }catch(Exception e){ e.printStackTrace(); e.getMessage(); } } /** * 根據條件生成Xls報表 * @param result Collection<Object> 結果集 * @param parameters Map 其他顯示的屬性 如列印事件 條件 等 可以為null * @param reportName 文件名字 * @throws Exception */ public void exportXls(Collection<?> result,String jrxmlName,String rptOutputPath,String reportName,HttpServletResponse resp){ try{ JasperReport jasperReport = null; if(jrxmlName != null && !jrxmlName.equals("")){ System.out.println("1"); String file = TEMPLATE + jrxmlName+ ".jrxml"; System.out.println(file); InputStream inputStream = this.getClass().getResourceAsStream(file); System.out.println(inputStream); JasperDesign design = JRXmlLoader.load(inputStream); jasperReport = JasperCompileManager.compileReport(design); } JasperPrint print = JasperFillManager.fillReport(jasperReport,null,new JRBeanCollectionDataSource(result)); if(System.getProperty("os.name").contains("Windows")){ System.out.println("2"); rptOutputPath = rptOutputPath.replaceAll("\\"+"\\", "\\"+"\\"+"\\"+"\\"); File file1 = new File(rptOutputPath); if(!file1.exists()){ file1.mkdirs(); }else{ if(!file1.isDirectory()){ file1.mkdirs(); } } rptOutputPath=rptOutputPath+"\\"+ reportName; }else{ System.out.println("3"); File file1 = new File(rptOutputPath); System.out.println(file1.toString()); if(!file1.exists()){ file1.mkdirs(); }else{ if(!file1.isDirectory()){ file1.mkdirs(); } } rptOutputPath = rptOutputPath+File.separator+reportName; System.out.println(rptOutputPath); } File file = new File(rptOutputPath); System.out.println(file.getParent()); File destFile = new File(file.getParent(), reportName); ByteArrayOutputStream oStream = new ByteArrayOutputStream(); JRXlsExporter exporterXls = new JRXlsExporter(); exporterXls.setParameter(JRExporterParameter.JASPER_PRINT, print); exporterXls.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); //exporterXls.setParameter(JRExporterParameter.OUTPUT_FILE, file); exporterXls.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8"); exporterXls.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream); //exporterXls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporterXls.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE); //删除多余的ColumnHeader需要加上参数 exporterXls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE); //删除记录最下面的空行需要加上参数 exporterXls.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); System.out.println("4"); exporterXls.exportReport(); byte[] bytes = oStream.toByteArray(); ServletOutputStream ouputStream = resp.getOutputStream(); resp.reset(); resp.setContentType("application/x-download"); resp.setContentLength(bytes.length); resp.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(reportName, "utf-8")); oStream.close(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); System.out.println("5"); }catch(Exception e){ e.printStackTrace(); e.getMessage(); } }