iReport输出为HTML或PDF、XLS、DOC【输出HTML两种方法】

时间:2024-04-10 11:24:31


iReport输出为HTML或PDF、XLS、DOC【输出HTML两种方法】

 



iReport输出为HTML或PDF、XLS、DOC【输出HTML两种方法】

//ireport 导出word格式
//导出word格式 在它的文档中也没有,

// String sql = "select * from cfg_static_user_relation"; 如果传SQL语句的话用该句

String ReportModel ="/test/test.jasper";
    File reportFile = new File(application.getRealPath(ReportModel));
    if(!reportFile.exists())
         throw new JRRuntimeException("报表绘制失败,找不到报表配置文件!");
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
             
// Connection conn=null;
//   conn=JdbcConnectionFactory.getConnection();
    Collection coll = UserService.generalCollecion();
JRDataSource sor =   new JRBeanCollectionDataSource(coll);
System.out.println(application.getRealPath("test/1224215057359.png"));
Map parameters = new HashMap();
    
       parameters.put("re",application.getRealPath("test/1224215057359.png"));
//     JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport ,parameters,conn);

        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport ,parameters,sor);
        JRExporter exporter = new JRRtfExporter();

        String tmpFilename = System.currentTimeMillis() + ".doc";
   response.setContentType("application/msword;charset=utf-8");
   response.setHeader("Content-Disposition", "attachment; filename="+tmpFilename);
      
 
   exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
   exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());    

        exporter.exportReport();
     

//这样就可以导出WORD格式了 

 

jasperreport + ireport 导出各种类型文件(word,excel,html,pdf,打印)

http://blog.csdn.net/flyzing/article/details/5218404


 

1.如下:

public class TestReport {

	  
     @SuppressWarnings("unchecked")
	public static void main(String []args){
    	// String reportPath = "D:\\compilation\\Test.jasper" ;  
    	 String reportPath = "D://workspace//cloudtaps-0.1//hot-deploy//opentaps-common//webapp//files//Test.jasper";
         Map parameters = new HashMap();
         // 如果报表中有用到变量,在这里给它赋值.
         parameters.put("orderby", "totalFee");  
         System.out.println(reportPath);
         String url="jdbc:mysql://www.runrungo.com/openb2c";

         try {
			Class.forName("com.mysql.jdbc.Driver");			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         Connection conn;
		try {
			conn = DriverManager.getConnection(url, "admin", "zslhDEjq,byld!");
			// Load编译好的模板   
			JasperReport jasperReport = (JasperReport) JRLoader.loadObject (reportPath);
			// 进行数据填充  
	         JasperPrint jasperPrint = JasperFillManager.fillReport (jasperReport, parameters, conn);
	         
	         
	         //预览
	         JasperViewer jrview = new JasperViewer(jasperPrint);    
	         System.out.println("jrview==="+jrview);
	         jrview.setPreferredSize( new  Dimension(200,100));
	         jrview.setVisible( true ); //这句控制弹出 
	         //导出为Html
	         //JasperExportManager.exportReportToHtmlFile(jasperPrint, "c:/area.html"); 

	         
	         //导出为pdf
	         //OutputStream output = new FileOutputStream(new File("c:/catalog.pdf"));
	         //JasperExportManager.exportReportToPdfStream(jasperPrint,output);
	         
		
	           
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

     }

}
2、
输出数据到HTML页面:
      JRHtmlExporter exporter = new JRHtmlExporter();         
                 StringBuffer sbuffer = new StringBuffer();
             
             exporter.setParameter(JRExporterParameter.PAGE_INDEX,pageIndex);             
             exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
             exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);
             exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);         
             exporter.exportReport();
             context.put("content",sbuffer.toString());
输出图片到HTML页面:
   JRHtmlExporter exporter2 = new JRHtmlExporter();    
             String sbuffer2=path+"/test.html";
             File file=new File(sbuffer2);             
             exporter2.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrintChart);             
             exporter2.setParameter(JRExporterParameter.OUTPUT_FILE,file);
             exporter2.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR,   Boolean.TRUE);             
             exporter2.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME,path+"/temp/");              
             exporter2.setParameter(JRHtmlExporterParameter.IMAGES_URI,path+"/temp/");                                        
             exporter2.exportReport();   
            context.put("path","/openb2c_reports/temp");

 

注意参考:

http://javaliujie.iteye.com/blog/278936

http://blog.csdn.net/lldwolf/archive/2008/05/19/2458278.aspx

http://www.iteye.com/topic/403478

 

 

Ireport:http://blog.csdn.net/lldwolf