XSSFWorkbook.setActiveSheet() equivalent in JasperReports? - apache-poi

We're using JasperReports 6.13.0 to generate XLSX format reports. We have a requirement where we need to show a particular sheet as default visible(instead of the first sheet) whenever the user opens the generated XLSX report.
My question is, do we have this feature in JasperReports? I couldn't find anything related to it in the documentation.
Note: In Apache POI, we can achieve this functionality using: XSSFWorkbook.setActiveSheet(int index)

JasperReports doesn't currently include this feature.
You can consider patching the JasperReports code to add it. What you would need to do is to add something like the following line in XlsxWorkbookHelper.exportHeader after the line with workbookPr.
write("<bookViews><workbookView activeTab=\"" + activeSheet + "\"/></bookViews>\\n");
You'd also need to pass the active sheet index somehow from the exporter.
Alternatively, you can export to XLSX using JasperReports and then open the file with POI and set the active sheet.

Related

POI XSSF - Can't add pictures to existing xlsx file

At this line:
wbk.addPicture(iconData, Workbook.PICTURE_TYPE_PNG);
I get this exception:
org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException: A part with the name '/xl/media/image2.png' already exists : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]
That same line of code has worked perfectly before. If I open the xlsx file with 7zip, there is actually a image named image2.png, which is correct and is another picture added by hand with Excel. It's like it tries to add the new image with the same name as the existing image. What am I doing wrong?
I was editing a file that was saved with LibreOffice 6.1, after opening it in Excel and saving it, it worked.
Also, like #Gagravarr said, a upgrade will be better and will probably work without needing to use Excel only

Apache poi sparklines

there is a "new" feature in Excel 2010 called sparklines. I could not find something about it regarding Apache POI. It looks like Apache POI does not support this feature. Am I right ?
Looks like this:
https://goo.gl/djnQSX
Thanks a lot!
You are completely right!
The Apache POI library has no support for SparkLines or other kind of chart as it's explained in their limitations page.
However if you just want to change/read/write the data behind the chart you can use POI anyway.
If you want or need something else like change the style or the range with chart data, it's no so hard to make some code to manage this kind of graphs.
Just by doing the next:
Change the extension of the Excel file from 'xlsx' to 'zip'
Unzip the file content.
Identify the number of the Sheet where your Sparkline is by editing the file 'xl/workbook.xml' and looking for a text similar to: ""
Then edit the file with that sheet info ('xl/worksheets/sheet3.xml' in the example).
Look for the code of the SparkLines (usally at the end of the file). It should looks like this:
<ext uri="{05C60535-1F16-4fd2-B633-F4F36F0B64E0}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">
<x14:sparklineGroups xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">
<x14:sparklineGroup displayEmptyCellsAs="gap">
<x14:colorSeries theme="4" tint="-0.499984740745262"/>
<x14:colorNegative theme="5"/>
<x14:colorAxis rgb="FF000000"/>
<x14:colorMarkers theme="4" tint="-0.499984740745262"/>
<x14:colorFirst theme="4" tint="0.39997558519241921"/>
<x14:colorLast theme="4" tint="0.39997558519241921"/>
<x14:colorHigh theme="4"/>
<x14:colorLow theme="4"/>
<x14:sparklines>
<x14:sparkline>
<xm:f>Sheet3!A2:A25</xm:f>
<xm:sqref>B3</xm:sqref>
</x14:sparkline>
</x14:sparklines>
</x14:sparklineGroup>
</x14:sparklineGroups>
</ext>
There you can edit the style info and data of the sparkline chart as you want/need.
Save the changes.
Compress again with ZIP all the folders and files.
Change the extension to 'xlsx' again.
You can do all this tasks with code.
I hope this helps

How to get DataValidation in Apache POI 3.9?

I am using Apache POI to write and read excel files,my poi version is 3.9,but now I faced a problem and do not find a good solution to solve it.
First I am use poi to write some datas to the excel table and will use DataValidation to add some prompt information in the table header,as the picture shown below:
The value in the cell is ABSInterention,and the prompt title is wrxuoh.After the excel generated successfully,other users will add some modification to the data cell and I need to read and parse the excel file and I want to get the prompt value wrxuoh again.So my question is:
When I read excel file using POI3.9,how can I get all the DataValidations for the current sheet?
I am using HSSFWorkbook and HSSFSheet.I found there is a method called getDataValidations in the POI Sheet,but I can not find this method in my poi3.9 jar file and also can not find it in the POI3.9 javadoc.
any help would be grateful!
Promoting a comment to an answer - you can't
That functionality was added to Apache POI after the 3.9 release you mention in your question
As per the Apache POI Changelog, you need to upgrade to at least POI 3.11 beta 3 to use that feature. 3.11 final, when it comes out shortly, will be the first stable release to include it

JasperReports: How to define a header in Excel

I'm cleaning up a legacy application which uses Jasper Reports 3.1.2, and am trying to figure out how to define a header that shows up as the header in the generated Excel spreadsheet (under Page Setup, Header/Footer). The existing code already does this for the footer using:
exportParameters.put(JRXlsAbstractExporterParameter.LEFT_FOOTER, "My footer text");
I can't find an equivalent parameter for the header in the JRXlsAbstractExporterParameter class.
Is there a way to do this in the JRXML template, or via the API ?
Adding a screenshot from the generated Excel report which shows that Excel detects the footer that was defined in the JRXlsAbstractExporterParameter class (code above). I am trying to do the same with the header.
Looking at JasperForge Config Reference, the JRXML header and footer properties are not supported until 3.7.5. In my opinion, it would be easier upgrading to at least version 3.7.5 to accomplish what you're attempting, update your jrxml files, and do regression testing with your existing reports. If upgrading is not possible and you're familiar with POI or JExcelAPI, I would suggest extending the JRXlsExporter or JRExcelApiExporter (limited to two links...sorry) and use custom exporter parameters in your API.
The version of Jasper Reports you're using really limits what you can do in excel. If you do upgrade, you may want to research the XLS properties available in versions above 4.0. If you're going to be doing more excel specific formatting with your existing reports, it would be more beneficial to uprade to a version higher than 4.0.

Import Excel outline (drill-down) structured data

Is it possible to import outlined data from Excel?
This is where Excel shows drill-down [+] [-].
Reading from ODBC doesn't give parent/child info; neither does exporting to CSV; export to HTML gives this info via mso-outline-level styles, but 2007 version puts actual content into subfolder and users will be VERY confused trying to select an appropriate file.
I ended up parsing .mht file exported from Excel, it has the required structure in form of tr tags and mso- attributes.
Excel 'Group and Outline' feature needs one column(or row) to know how to group rows, isn't it ?
Info about outline is stored in object. So neither ODBC nor CSV allow you to read it. You should use COM api in older versions of Excel or new C# api in latest to get access to those informations.

Resources