I am attempting to create a report with a crosstab that will ignore the width of the page when exported to Excel, but won't ignore the width of the page when exported to PDF. I am setting the report's ignore width property:
<propertyExpression name="net.sf.jasperreports.crosstab.ignore.width"><![CDATA[$P{Format}.equals("XLS") ? ("true") : ("false")]]></propertyExpression>
This is set at the top of the page with the other <property> tags. In my parameters, I also have:
<parameter name="Format" class="java.lang.String"/>
When we run this report with Java code and pass a value into the format parameter, this code never ignores the width of the page (even when we assign Format="XLS"). However, if I run this in Jaspersoft Studio, this code will work as long as I have isForPrompting="true" (if it is false and I assign a default value, this code also fails to work).
Is this because I am referencing the parameter before it is defined? If so, where should I move the property expression to get this code to work?
2 Answers:
I got this to work by setting the IS_IGNORE_PAGINATION parameter to false when our format was "PDF" and to true when our format was anything else:
if(format.equals("PDF")){ parameters.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.FALSE); } else { parameters.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE); }
Where "parameters" is the Map<String, Object> that is sent to Jasper.
I believe what I am trying to do is impossible, per a couple sources:
https://community.jaspersoft.com/questions/976451/set-report-properties-expression
So, I have transitioned to trying to complete this in Java code. I have run across this example that seems to be exactly what I want:
https://community.jaspersoft.com/questions/534046/excel-report-crosstab
However, for some reason this solution doesn't work for me (possibly due to the fact that the solution is 9 years old). I do not have access to the JasperReport object, but I have access to a JasperPrint object, which also has a setProperty(String, String) method. Trying to set "JRCrosstab.PROPERTY_IGNORE_WIDTH" to "true" doesn't do anything. I also tried to use an JRXlsExporter's getPropertyUtil() to get access to the propertyUtil's setProperty(String, String) method, but this also doesn't work.
Any suggestions on how to get this to work?
I can confirm that the "Format" parameter is being set, as toward the bottom of the page I have:
<printWhenExpression><![CDATA[!$P{Format}.equals("XLS")]]></printWhenExpression>
This works exactly as expected.