jasper reports loading report from jasper - java

I'm loading the jasper reports file from .jasper file to improve the performance as below.
JasperReport rpt = (JasperReport)JRLoader.loadObjectFromFile(location);
but I have a requirement now, that a query which is formed dynamically has to be set to .jasper (compiled) file. is there a way to do the same?

Something like this?
Java
JasperReport rpt = (JasperReport)JRLoader.loadObject(Your .jasper file path);
Map paramMap = new HashMap();
paramMap.put("p_sql", "select * from baztable where foo='bar' and fuga='hoge' order by username");
// pass the dynamic query to .jasper file.
JasperPrint print = JasperFillManager.fillReport(rpt, paramMap, connection);
jrxml
<parameter name="p_sql" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["select * from baztable order by username"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[$P!{p_sql}]]>
</queryString>

You can use the $P! (Parameter with exclamation) which can be used for conditional queries.
Using this you can craft dynamic queries based on certain conditions.
A guide for the same can be found here.

Just open .jasper file in to ireport so you can load .jasper file and edit it.

This is a hack method for the JRXML file. The JRXML file has your query. You can write a Java code or any other code to replace/write down your Query here:
Sample MongoDB Query:
<queryString language="MongoDbQuery">
{'collectionName':'customer'}
</queryString>`

Related

Report coming up blank using Java code, previews OK in the Studio [duplicate]

This question already has answers here:
Blank PDF even with the simplest Jasperreport jrxml
(3 answers)
Jasper report exports empty data in PDF format when there is more data
(5 answers)
Closed 4 years ago.
Using a Jaspersoft Studio 6.6.0 I created a very simple report with a static text and a red rectangle (as well as many other things I tested before). There is no query to fetch data, no dynamic field or anything.
This is what the design looks like:
Preview looks fine in all different formats (HTML, PDF etc..)
The problem I have comes when I try to generate the report in my web app. Using a library version 6.7.0.
HTML Export coded like this yields an empty String
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
StringBuilder htmlStringBuilder = new StringBuilder();
HtmlExporter exporter = new HtmlExporter();
SimpleHtmlExporterConfiguration conf = new SimpleHtmlExporterConfiguration();
conf.setHtmlHeader("");
conf.setBetweenPagesHtml("");
conf.setHtmlFooter("");
conf.setFlushOutput(true);
exporter.setConfiguration(conf);
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(htmlStringBuilder);
exporter.setExporterOutput(output);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.exportReport();
return htmlStringBuilder.toString(); // empty string
Eport to PDF using creates a single page PDF file. The page is blank.
JasperExportManager.exportReportToPdfFile(jasperPrint, "file.pdf");
I tried to compile the JRXML file using both the Studio and also in the Java code. The result is still the same. JasperPrint object seem to be populated, however no elements are present on the page, which I reckon is related. Any chance to find out why?
I see two options for this:
In your java code import the net.sf.jasperreports.engine.JREmptyDataSource class and fill the jasperPrint like so:
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
This should guarantee that your detail band is displayed once. This is what JasperSoft Studio uses when you preview a report with One Empty Record Data Adapter.
In JasperSoft Studio, move all the content out of the Detail band to Summary band, for example, and in Report Properties tab select:
When No Data Type: All Sections No Detail
This way all the other bands, except the Detail one, will be rendered when you don't provide any data.

Error in loading object from InputStream in java while trying to open a jasper report

I have a subreport which I have used in my main jasper report, I am sending the subreport from a java form using an InputStream, this is the code:
InputStream suprepo = getClass().getResourceAsStream("LinuxTest_subreport1.jasper");
And in the main report I have create a parameter that's class is InputStream, and accepting the value from my java form that I'm passing using a hashmap.
My problem is that everything works fine when there is only 1 page, but as soon as there is more then one page I receive this error:
error in loading object from input stream
This is my jasper code
<parameter name="subrepopath" class="java.io.InputStream" isForPrompting="false"/>
I also suffered from this issue and just recently found the solution thanks to this post.
What you need to do is change your subreport type to Object:
<parameter name="subrepopath" class="java.lang.Object" isForPrompting="false"/>
In your SubReport properties, set the Expression class to be:
net.sf.jasperreports.engine.JasperReport
In your code, you need to load the object into a JasperReport object, this is where I differ from the linked page as the methods used are deprecated.
So you would do:
InputStream suprepo = getClass().getResourceAsStream("LinuxTest_subreport1.jasper");
JasperReport subJasperReport = (JasperReport)JRLoader.loadObject(suprepo);
Then add in your parameter map the object subJasperReport with the key of subrepopath and you'll find your multi page report will now generate correctly.
I had the same issue, and it was caused by a subreport (.jasper) compiled with other version of the Jasper Report.

Number Stored as Text in Excel created using DynamicReports

I am using Dynamic Reports to create Reports and am able to create it. But the issue is that when i convert it to excel, the cell in the excel showing a warning as Number Stored as Text. Because of this no operation is possible.
Here is my code
File file = new File("c:/report.xls");
JasperXlsExporterBuilder xlsExporter = export.xlsExporter(file).setDetectCellType(true).setIgnorePageMargins(true)
.setWhitePageBackground(false).setRemoveEmptySpaceBetweenColumns(true);
report.addProperty(JasperProperty.EXPORT_XLS_FREEZE_ROW, "2").ignorePageWidth().ignorePagination().toXls(xlsExporter);
How to remove this error.
Add the following line to your code:
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
Source
EDIT
In the Dynamic Reports samples, the report variable also sets the following. This could help depending on the column.
.columns(
itemColumn,
col.column("Quantity", "quantity", type.integerType()),
col.column("Unit price", "unitprice", type.bigDecimalType()))

How to generate a doc file using Jasper Report

Using jasper-reports 5.6.1 I am able to generate the reports in pdf format, but I am not able figure out how to generate a .doc format by using jasper.
byte[] exportReportToPdf = JasperExportManager.exportReportToPdf(print);
is for generating a pdf format file like this is there any similar view class for doc format?
Try like this
JasperPrint jasperPrint = JasperFillManager.fillReport("myReport.jasper", reportParameters, dataSource);
Exporter exporter = new JRDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
File exportReportFile = new File("D:\\Temp\\report.docx");
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(exportReportFile));
exporter.exportReport();
HTH
Before executing a report, the JRXML must be compiled in a binary object called a Jasper file(*.jasper). This compilation is done for performance reasons. Jasper files are what you need to ship with your application in order to run the reports. Once the report is compiled it is filled with data from the application. The class net.sf.jasperreports.engine.JasperFillManager provides necessary functions to fill the data in the reports.
The report execution is performed by passing a Jasper file and a data source to JasperReports. There are plenty of types of data sources, it's possible to fill a Jasper file from an SQL query, an XML file, a csv file, an HQL (Hibernate Query Language) query, a collection of Java Beans, etc... If you don't find a suitable data source, JasperReports is very flexible and allows you to write your own custom data source.
JasperFillManager.fillReportToFile( "MasterReport.jasper" , parameters, getDataSource());
This operation creates a Jasper print file (*.jrprint), which used to either print or export the report.
- See more at: http://blog.manupk.com/2012/11/using-jasper-reports-to-create-reports.html#sthash.rFqV8K4i.dpuf

Do Jasper support xml file as the datasource?

I want to know do JasperServer accepts xml file as the report data source and whether we can perform same operation on xml nodes that we perform on the various data-source in adhoc report creator like dragging dropping etc.?
yes, jasper does support XML based datasources, you can create your report just the way you would create it using any other datasource. Jasper has an abstraction from datasources which you provide to ensure it behaves the same (or atleast similarly) with different datasources.
look here and here for some additional info
Here is the sample Code I tested for xml file as data source and out is PDF report:
JasperReport jasperReport = JasperCompileManager.compileReport("<Path of jrxml file>");
JRXmlDataSource xmlDataSource = new JRXmlDataSource("<Path of xml file>", "XPath Query");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), xmlDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "<Path of file PDF output file>");

Categories

Resources