I am currently working on a project that uses a nat table to display data to a user. I am wanting to add an option to export this nat table to a csv file or an excel document. Is there an easy way to export to excel or must I find a way to do it manually? If I must do it "the hard way" can anyone point me somewhere to help me get started on exporting to Excel?
Thanks.
NatTable itself comes with a default exporter to Excel.
The exporter class is ExcelExporter (in package org.eclipse.nebula.widgets.nattable.export.excel).
An easy way to use this would be with ExportCommand (in package org.eclipse.nebula.widgets.nattable.export.command). With the default bindings, this results in a file chooser in which the user can specify a *.xls file.
ExportCommand cmd = new ExportCommand( m_table.getConfigRegistry(), m_table.getShell());
m_table.doCommand( cmd );
With version NatTable 0.9 on my system, when opening the file, Excel shows a warning that the file is in a different format than specified by the file extension.
NatTable comes with an Excel Exporter based on Apache POI already. You need to add the POI extension from the NatTable project to your project for this.
Doing this gives you the opportunity to use the HSSFExcelExporter which produces a valid Excel file (the default ExcelExporter simply creates a XML format) and comes with additional configuration possibilities.
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORTER, new HSSFExcelExporter());
for reading / writing to excel you can use Apache POI http://poi.apache.org/
for how to get started with Apache POI look at http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/ might help
Related
I have to upload XLSX files to a web application. This web application uses CodeIgniter to check and import those files.
Furthermore, I have to generate these XLSX files from another one. In order to read and write XLSX files, I use Apache Poi. This part is pretty easy and works well.
But, here is my problem: when uploading an auto-generated file, CodeIgniter decline the files saying that this file type is not allowed. It's probably a missing property that's not created by Apache POI library but I didn't managed to find which one.
Another 'fun' fact is that when opening an Apache Poi auto-generated file with Microsoft Excel then saving it without any modification the file gains something like 3Ko of data and becomes valid for CodeIgniter. It doesn't work with LibreOffice Calc which apparently adds some data but not the same as Microsoft Excel do.
Do you have any idea of which property or data could be missing? Any method to resolve my problem?
Edit: after some more investigations and according to php finfo_file function (used by CodeIgniter) my bad file has following mime type application/octet-stream while a legit file has following mime type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. Then, I think that Apache Poi has some bug when generating XLSX.
Edit 2: Finally, there's 2 XLSX type (see enclosed screenshot). Only the second one is recognized as an application/vnd.openxmlformats-officedocument.spreadsheetml.sheet by finfo_file. Unfortunately, Apache Poi generates signature of the first type. Thus, it isn't recognized as a XLSX.
This strange behaviour finally comes from the ambiguity of the XLSX file format. It covers two different file formats (as you can see on the enclosed picture from https://www.filesignatures.net/). Only the second one is recognized as an application/vnd.openxmlformats-officedocument.spreadsheetml.sheet by finfo_file whereas Apache Poi generates files of the first type. Thus, the upload fails saying the file type is not allowed.
Is there a way to convert a xls file into a pdf?
I want to make a dynamical report directly to pdf file, but didn't find a way to make dynamic columns on iReport, so I've made a method on Java that exports to xls dynamically.
So I was wondering if is there a way to convert this file to pdf, but it need to be on a method from my code. Or if you have a better idea, it can be used too.
Maybe there's a way to make this pdf file from my code as I did with xls. Please help me out.
Thanks.
Try using iText http://itextpdf.com/ - I've used it to create PDFs with columnar structure.
In addition to using iText directly, there are a couple report engines that sit on top of it:
Eclipse BIRT (using 2.1.7, the last MPL/LGPL version)
Jasper Reports (which uses a Very Old version, 1.3.1 IIRC)
This is a commercial solution:
http://www.dancrintea.ro/xls-to-pdf/
If you want open source try jakarta POI.
Try using the Muhimbi PDF Converter Services. It comes with a Java compatible Web Services based interface and sample code. It does other things as well.
I worked on this application, so the usual disclaimer applies.
I have a directory where files are constantly updated. I need to read the latest excel file and convert it into tab delimited file. It is under windows. A batch + java solution will work for me. Or if I can use excel in command line programatically that also works
I would disagree with those people who recommend Apache POI. The best API that I know of for dealing with Excel is Andy Khan's JExcel.
It has already been widely suggested, POI is probably the most complete "pure Java" implementation of Excel.
In one API you get support for Excel 2003 and 2007.
However, you need to be weary of its memory footprint. It is a hog. If you use it, make sure you use the event-driven model it supports as this will reduce footprint and execute faster.
In Java you can use, for example, Apache POI library to read data from Excel files. And then use standard Java facilities to write data into tab delimited file.
You can read in the excel sheet using POI and then iterate through the cells, writing them out to a separate file with appropriate delimiters.
Have a look at other SQ question: convert Excel to csv either using shell script or jython . if so how
I answered with PyODConverter, but there is jodconverter: Java version of this tool. It uses OpenOffice working as a service. I use it to convert between various file formats.
You can use Apache POI API for reading the excel file and OpenCSV for writing the CSV file.
I want to convert .doc/.xls file into PDF through my java application. Now, I know there have been some discussions on this topic. I also know that JODConverter is one option, but the problem with this is dependency on OpenOffice. Whoever wants to use my app will first need to install OpenOffice on his/her host. I want to avoid this. Do we have any new solution through which I can do this conversion in my app without having dependencies on any other applications.
You could use POI for reading XLS and DOC, and use iText for generating the PDF.
POI project:
http://jakarta.apache.org/poi/
iText project:
http://www.lowagie.com/iText
I need to determine which version of Microsoft Excel is installed on a Windows PC. I'm working with Apache POI but can't figure out an easy way to do this.
Anyone know if Apache POI has a built in functionality to do this? Any ideas how to do this using Java code?
POI provides a mechanism for writing Excel documents without using any of the Excel APIs. I doubt it has a mechanism to detect the installed version, as it has no dependency on an installed version of Excel.
Following a pattern similar to the one described in this question about detecting the Excel version in .NET, you could use JACOB to detect the installed Excel version like so:
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try {
xl.getProperty("Version");
} finally {
xl.invoke("Quit", new Variant[] {});
}
I really, really don't think POI has this functionality, as it's explicitly written to work cross-platform. It only deals with document files.
If I were faced with this task, I'd dig into the registry (via the Windows API) for the association of the .XLS file type. That will, err should give you a link to the most recently and completely installed Excel. Not reliably, because
there might not be an Excel installed;
the user might have changed the association; or
the Excel pointed at by the association might be broken.
But given the hint of (usually) the applicable executable, you could then examine either the .EXE file header of Excel or perhaps something in the surrounding files to determine Excel's version.
Ok it just occured to me that older Microsoft Excel versions have file extensions .xls and new ones have .xlsx.
I can just get reference to the file and get the file extensions... that easy.
If anyone has a better solution please let me know.