日期:2014-05-17 浏览次数:20869 次
/**
* 生成報表的公用方法
* @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.OUTPU