Jasper报告与excel数据源 [英] Jasper report with excel data source

查看:358
本文介绍了Jasper报告与excel数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jasper设计器(不是iReport而是eclipse插件)创建了一个使用excel文件作为数据源的报告。

报告工作正常,在设计器中,并阅读来自文件的数据没有问题但是在将文件编译为 file.jasper 之后,给了他excel文件的路径,在JasperViewer中没有任何东西!

这是我的代码:

I have created, using a jasper designer (not iReport but a plugin for eclipse), a report that uses an excel file as datasource.
the report works fine, in the designer, and read the data from the file without problems but after compiling the file to file.jasper and giving him the excel file's path nothing apears in the JasperViewer!
This is my code:

try{
      Map<String, Object> parameters = new HashMap<String, Object>();
      parameters.put("DataFile", "jasper_export.xls");
      JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(new File("file.jasper")), parameters,conn);

      JasperViewer jv = new JasperViewer(jasperPrint, false);
      jv.setVisible(true);
    } catch (Exception ex) {
        ex.printStackTrace();
    }


推荐答案

解决方案:

此代码完美无缺:

SOLUTION:
This code woks perfect:

try{
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("param_name", paramValue);

    ExcelDataSource ds = new ExcelDataSource(JRLoader.getLocationInputStream(excelFilePath));
    String[] columnNames = new String[]{"id", "nom", "iden", "adress", "activity", "compta"};
    ds.setColumnNames(columnNames);
    JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(new File(yourJasperFilePath)), parameters, ds);
    JasperPrintManager.printReport(jasperPrint, false);
   } catch (Exception ex) {
      ex.printStackTrace();

   }

这篇关于Jasper报告与excel数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆