How to use csv as datasource in Jaspersoft Server?

2019-01-28 10:24发布

问题:

I've been asked to develop a report using Jaspersoft studio to generate the report template and then use jaspersoft server to have users that generate the report on demand, using data from a CSV file, instead of a DB.

I've been able to generate a report locally using studio, upload the report to the server, but I'm not able to generate the report from there.

I've been looking around and every solution that is proposed is to inject the data into a DB and then generate the report using that. But in other post in the jaspersoft community, it's said that CSV support is going to be added by early 2014, so I guess it's already there.

So, What I think that might fix it is:

1) Add the CSV file to the server, but how to link the report (.jrxml) and the dataAdapter (.xml)?

Right now, I've uploaded it to /contentFiles/xls/energy.csv, but if I put that on the data adaptor, jaspersoft can't find it:

I assume because there is no ip to the server, so it might think it's a local file. If I use the ip to the server, in my case, http://...***:8080/jasperserver/ (* for security reasons) and add after that /contentfiles/xls/energy.csv it still can find it. So, how to link the adaptor to the csv file on the server?

Also, since this csv file will be generated from time to time:

2) There is a folder where I can place or generate the csv file to be updated on the jaspersoft server?

回答1:

There are a couple of steps to follow in order to have a file-based dataAdapter correctly deployed in JasperReports Server. The following steps were performed with Jaspersoft Studio 6.4.0 and JasperReports Server Pro 6.4.0.

  1. Assuming you have your CSV file in your workspace

(I'm using the CsvDataSouce.txt from the csvdatasource sample)

you begin by creating the data adapter from the Repository Explorer view by pointing to your file and setting up the column names.

  1. You then export your newly created adapter to file by right-clicking it and choosing the Export To File option (I'm saving it along the CSV file)

  2. At this point you could even delete the adapter from the Repository Explorer view and keep only the saved file to avoid any mistakes. You then create your report by picking the workspace dataAdapter and continue with your report design.

  3. Publishing the report to the server at this stage would not push the dataAdapter files. You need to specify the report's Default Data Adapter property from the properties panel

by picking your workspace adapter

  1. You can now publish the report to your server and the wizard will automatically discover the additional resources to be published

Make sure you pick the Don't use any Data Source option in the last dialog

  1. Your report is now linked to the one from the server with this set of properties:

    <property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver/"/>
    <property name="ireport.jasperserver.user" value="jasperadmin"/>
    <property name="ireport.jasperserver.report.resource" value="/public/CsvReport_files/main_jrxml"/>
    <property name="ireport.jasperserver.reportUnit" value="/public/CsvReport"/>
    

You can continue to work on it and Jaspersoft Studio will prompt you to republish on each save.

  1. Your CSV file is now a JasperReports Server repository resource that you can update whenever you want, either through Studio or Server's UI. If you don't want it to reside on the JasperReports Server side you could simply use an external URL pointing to your file when creating/editing the dataAdapter file.