Jasper report with excel data source

2019-09-04 12:03发布

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();
    }

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-04 12:39

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();

   }
查看更多
登录 后发表回答