Jump to content
We've recently updated our Privacy Statement, available here ×

Pass CSV DataSource from java program to Report's Tables


emanuele.a89

Recommended Posts

I have a report made of one page which is generated usign a CSV Data Adapter. In the report I have some data and 2 table where both are taking data from CSV file. In a first moment, I had problem with table showing up: I put some data in myTables but nothing were shown. I have been able to solve the problem by using and mapping REPORT_PARAMETER_MAP in ParameterMap table’s section. Now I have the same problem, but calling myReport from a java program. In this last I have created a datasource which I am sending as paramater to the report. As a result, all data are showned, unless the 2 tables. In Jaspersof Studio I have defined a parameter called DS where its class is: “new net.sf.jasperreports.engine.data.JRCsvDataSource” and I am passing it to myTables as follow:

 

ds2.png.7a6a96837cc6bb7b1b7213d7a51f2679.pngIn java I have:

 

JasperPrint jasperPrint = null;    try    {        String[] columnNames = new String[]{"xxx3", "xxx4", "xxx2", "xxx1"]        JRCsvDataSource ds = new JRCsvDataSource(inp);        ds.setFieldDelimiter(';');        ds.setUseFirstRowAsHeader(true);        ds.setColumnNames(columnNames);        _parameters.put("DS", ds);        jasperPrint = JasperFillManager.fillReport(_jasperReport, _parameters, ds);    }    catch (Exception e)    {        putKOReport(outputStreamEnvelopeFactory,e.toString().getBytes());;        e.printStackTrace();        writeErrorLog("OPS: readReportConfiguration " + e.getMessage() + " " + e);        throw new RuntimeException(e);    }    return jasperPrint;}public void prepareReport() throws SQLException, OutputNotFoundException, OutputEnvelopeCreationException{            _parameters.put("PARAM1", "/main/prj/xxx1.png");            _parameters.put("PARAM2", "/main/prj/xxx2.jpg");            _parameters.put("PARAM3", "/main/prj/xxx3.jpg");            _parameters.put("PARAM4", "/main/prj/xxx4.jpg");    try    {        _jasperReport = JasperCompileManager.compileReport(pathReportJasper);        writeInfoLog("OPS: Report Configuration "+pathReportJasper+" has been prepared");    }    catch (Exception e)    {        putKOReport(outputStreamEnvelopeFactory,e.toString().getBytes());;        e.printStackTrace();        writeErrorLog("OPS: prepareReport " + e.getMessage() + " " + e);        throw new RuntimeException(e);    }}public byte[] writeOutputFile(JasperPrint jasperPrint){    byte[] reportPdf = null;    try    {        reportPdf = JasperExportManager.exportReportToPdf(jasperPrint);    }    catch(Exception e)    {        putKOReport(outputStreamEnvelopeFactory,e.toString().getBytes());        e.printStackTrace();        writeErrorLog("OPS: writeOutputFile " + e.getMessage() + " " +e);        throw new RuntimeException(e);    }    return reportPdf;}

I am doing something wrong? Does anyone could help me, please?

Thank you in adavance!

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

Same answer as posted here: https://stackoverflow.com/questions/60167856/pass-csv-datasource-from-java-program-to-reports-tables/60185299#60185299

 

You are reusing the same data source for filling the main dataset and the tables:

JRCsvDataSource ds = new JRCsvDataSource(inp);..._parameters.put("DS", ds);jasperPrint = JasperFillManager.fillReport(_jasperReport, _parameters, ds);

This has unpredictable results because of the multiple consumers for the same data source.

If you have control over the InputStream source like the path to the file you could pass that as a parameter and construct a new instance of JRCsvDataSource for each table that needs it.

Link to comment
Share on other sites

0

 

I have been able to solve the problem: I created a datasource for each table using a cycle from my java program and creating a JRCsvDataSource parameter for each one them. This way all my Tables are getting printed with data. In JRDataSource expression I had to cast the object $P{DS} this way:

((net.sf.jasperreports.engine.data.JRCsvDataSource)$P{DS})[/code]

otherwise tables were not printed. The object alone does not work!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...